0

[enter image description here][1]I am trying to create a lowry plot in R but am having difficulty debugging the errors returned. I am using the following code to create the plot:

   library(ggplot2)
   library(reshape)

m_xylene_data <- data.frame(
Parameter = c(
"BW", "CRE", "DS", "KM", "MPY", "Pba", "Pfaa", 
"Plia", "Prpda", "Pspda", "QCC", "QfaC", "QliC", 
"QPC", "QspdC", "Rurine", "Vfac", "VliC", "Vmax"),
"Main Effect" = c(
1.03E-01, 9.91E-02, 9.18E-07, 3.42E-02, 9.27E-3, 2.82E-2, 2.58E-05, 
1.37E-05, 5.73E-4, 2.76E-3, 6.77E-3, 8.67E-05, 1.30E-02, 
1.19E-01, 4.75E-04, 5.25E-01, 2.07E-04, 1.73E-03, 1.08E-03),
Interaction = c(
1.49E-02, 1.43E-02, 1.25E-04, 6.84E-03, 3.25E-03, 7.67E-03, 8.34E-05, 
1.17E-04, 2.04E-04, 7.64E-04, 2.84E-03, 8.72E-05, 2.37E-03, 
2.61E-02, 6.68E-04, 4.57E-02, 1.32E-04, 6.96E-04, 6.55E-04
)
)
fortify_lowry_data <- function(data, 
                           param_var = "Parameter", 
                           main_var = "Main.Effect", 
                           inter_var = "Interaction")
{
 #Convert wide to long format
mdata <- melt(data, id.vars = param_var)

#Order columns by main effect and reorder parameter levels
o <- order(data[, main_var], decreasing = TRUE)
data <- data[o, ]
data[, param_var] <- factor(
data[, param_var], levels = data[, param_var]
)

#Force main effect, interaction to be numeric
 data[, main_var] <- as.numeric(data[, main_var])
 data[, inter_var] <- as.numeric(data[, inter_var])

  #total effect is main effect + interaction
  data$.total.effect <- rowSums(data[, c(main_var, inter_var)])

  #Get cumulative totals for the ribbon
  data$.cumulative.main.effect <- cumsum(data[, main_var])
 data$.cumulative.total.effect <- cumsum(data$.total.effect)

 #A quirk of ggplot2 means we need x coords of bars
  data$.numeric.param <- as.numeric(data[, param_var])

 #The other upper bound
 #.maximum =  1 - main effects not included
 data$.maximum <- c(1 - rev(cumsum(rev(data[, main_var])))[-1], 1)

  data$.valid.ymax <- with(data, 
                       pmin(.maximum, .cumulative.total.effect)
  )

  mdata[, param_var] <- factor(
   mdata[, param_var], levels = data[, param_var]
  ) 
  list(data = data, mdata = mdata)
  }
    lowry_plot <- function(data,
                   param_var = "Parameter",
                   main_var = "Main.Effect",
                   inter_var = "Interaction",
                   x_lab = "Parameters",
                   y_lab = "Total Effects (= Main Effects + Interactions)",
                   ribbon_alpha = 0.5,
                   x_text_angle = 25)
  {
  #Fortify data and dump contents into plot function environment
 data_list <- fortify_lowry_data(data, param_var, main_var, inter_var)
  list2env(data_list, envir = sys.frame(sys.nframe()))

   p <- ggplot(data) +
    geom_bar(aes_string(x = param_var, y = "value", fill = "variable"),
         data = mdata) +
     geom_ribbon(
      aes(x = .numeric.param, ymin = .cumulative.main.effect, ymax = 
    .valid.ymax),
     data = data,
     alpha = ribbon_alpha) +
   xlab(x_lab) +
   ylab(y_lab) +
   scale_y_continuous(labels = "percent") +
   theme(axis.text.x = text(angle = x_text_angle, hjust = 1)) +
     scale_fill_grey(end = 0.5) +
    theme(legend.position = "top",
     legend.title =blank(),
     legend.direction = "horizontal"
     )
   p
   }

m_xylene_lowry <- lowry_plot(m_xylene_data)

When I run the code, it is giving me the following error:

Error: argument "x" is missing, with no default

It is not specific enough for me to know what the issue is. What is causing the error to be displayed and how can I make error statements more verbose?

Lowry PLOT

Community
  • 1
  • 1
LOCKI_suj
  • 11
  • 1
  • 4
  • 1
    @SUJANGHIMRE, "It is not specific enough for me to know what the issue is. What is causing the error to be displayed", Rightly said, for its a pain to debug this mountain of code! See this [thread](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to learn and understand how to make a reproducible example. – mnm Dec 28 '17 at 06:06
  • 1
    It's specifically the `text()` call in the following line: `theme(axis.text.x = text(angle = x_text_angle, hjust = 1))`. However, I have to say there's quite a few other errors in your code. `theme(legend.title =blank()` is not going to work either. I would strongly suggest rebuilding your code step by step to make sure it's working fine instead of creating custom functions. – Phil Dec 28 '17 at 06:18

1 Answers1

3

It seems that you have more than one faulty element in your code than just the error it throws. In my experience it always helps to first check whether the code works as expected before putting it into a function. The plotting-part below should work:

p <- ggplot(data) + # no need to give data here, if you overwrite it anyway blow, but does not affect outcome...
    # geom_bar does the counting but does not take y-value. Use geom_col:
    geom_col(aes_string(x = param_var, y = "value", fill = "variable"),
             data = mdata,
             position = position_stack(reverse = TRUE)) +
    geom_ribbon(
      aes(x = .numeric.param, ymin = .cumulative.main.effect, ymax = 
            .valid.ymax),
      data = data,
      alpha = ribbon_alpha) +
    xlab(x_lab) +
    ylab(y_lab) +
    # use scales::percent_format():
    scale_y_continuous(labels = scales::percent_format()) +
    # text is not an element you can use here, use element_text():
    theme(axis.text.x = element_text(angle = x_text_angle, hjust = 1)) +
    scale_fill_grey(end = 0.5) +
    # use element_blank(), not just blank()
    theme(legend.position = "top",
          legend.title = element_blank(),
          legend.direction = "horizontal"
    )

This at least plots something, but I'm not sure whether it is what you expect it to do. It would help if you could show the desired output.

Edit:

Added position = position_stack(reverse = TRUE) to order according to sample plot.

Tino
  • 2,091
  • 13
  • 15