0

I have a plot, that i repeat numerous times, just with different variables, so i want to make it into a function:

getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){

  sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
    geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
    geom_line(aes_string(y = yvarsec), color = groupvar) 
}

test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"), yvar = "severity", 
yvarsec = "frequency", groupvar = "gender")

I get an error:

Unknown colour name: gender

I assume that this is because the color is evaluated as color = "gender" because of the aes_string.

How can this be fixed? I tries using enquo() and !!, but it has not worked. Any tips?

also related to this, i want to scale the yvarsec by a parameter. I have done thee following:

getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){

  param <- max(as.numeric(freqSevData$yvar), na.rm = TRUE)/max(as.numeric(freqSevData$yvarsec), na.rm = TRUE)

  sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
    geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
    geom_line(aes_string(y = as.numeric(yvarsec) * as.numeric(param)), color = groupvar) +
    theme_pubclean()

}

which return a plot without the line. Again, i believe this is related to the NSE and aes_string.

without the as.numeric i get an error:

Error in yvarsec * param : non-numeric argument to binary operator

Proposed solution as by @linog

getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){

    df[,"param"] <- max(as.numeric(data[, yvar]), na.rm = TRUE)/max(as.numeric(data[, yvarsec]), na.rm = TRUE)
    df[,"param"] <- df[,"param"]* df[, yvarsec]

  sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
    geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
    geom_line(aes_string(y = "param", color = groupvar)) +
    theme_pubclean()

}

test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"), 
yvar = "severity", yvarsec = "frequency", groupvar = "gender")
    test
Nneka
  • 1,764
  • 2
  • 15
  • 39

1 Answers1

0

I think this is because your color parameter is out of the aes_string. You should correct to aes_string(y = yvarsec, color = groupvar).

Try with the correction below.

By the way, I think you should not use get in your function call since you don't want name to be matched with a value (this is the aes_string that does that for you)

getSecPlot <- function(data, xvar, yvar, yvarsec, groupvar, ...){

param <- as.numeric(
  freqSevData[, max(get(yvar), na.rm = TRUE)/max(get(yvarsec), na.rm = TRUE)]
)
df[,"param" := get(yvarsec)*param]

  sec_plot <- ggplot(data, aes_string (x = xvar, group = groupvar)) +
    geom_col(aes_string(y = yvar, fill = groupvar), position = "dodge") +
    geom_line(aes_string(y = "param")) +
    theme_pubclean()
}

test <- getSecPlot(freqSevDataAge, xvar = get("agegroup"), yvar = "severity", 
yvarsec = "frequency", groupvar = "gender")
linog
  • 5,786
  • 3
  • 14
  • 28
  • that is true about `get`, but that one parameter is an exception which i need...and yes, that solves the issue *facepal*, thanks ... also, i edited a send part to the question, any ideas? – Nneka Apr 15 '20 at 18:28
  • I edited the answer. I recommend you to first compute your column and stick with SE by using `aes_string(y = "param")`. Does it answer your question ? – linog Apr 15 '20 at 18:36
  • i get en error with the proposed solution: `Error in `[.data.table`(data, , yvar) : j (the 2nd argument inside [...]) is a single symbol but column name 'yvar' is not found. Perhaps you intended DT[, ..yvar]. This difference to data.frame is deliberate and explained in FAQ 1.1.` – Nneka Apr 15 '20 at 18:46
  • i included it in my question, am i doing something wrong there? – Nneka Apr 15 '20 at 18:52
  • You had not mentioned you were using `data.table`. SE is easier with `data.table` (see a [blog poste I wrote on the subject](https://linogaliana.netlify.com/post/datatable/datatable-nse/)). I proposed you an answer – linog Apr 15 '20 at 18:53