ggplot using column names using the tidy evaluation pronoun .data fails

I'm struggling to call dataframe columns using strings for ggplot 3.3.6. The code does not generate any error message, just the pdf file is empty.

input data is N x M dataframe, N is the number of subjects, M has columns such as "age", "height", "weight", etc.

fscatter <- function(input, x, y){
  pdf(filen, width=10, height=10)
  ggplot(input, aes(x = .data[[x]], y = .data[[y]]))  + 
    geom_point() + 
    geom_smooth(formula = y ~ x, method=lm) + 
    theme1
  dev.off()
}

fscatter(df,"age","height")

any idea?

Maybe

fscatter <- function(input, x, y){
  pdf(filen, width=10, height=10)
  ggplot(input, aes_string(x = x, y = y))  + 
    geom_point() + 
    geom_smooth(formula = y ~ x, method=lm) + 
    theme1
  dev.off()
}

fscatter(df,"age","height")

I suggest you use ggsave()

fscatter <- function(input, x, y){
  ggplot(input, aes(x = .data[[x]], y = .data[[y]]))  + 
    geom_point() + 
    geom_smooth(formula = y ~ x, method=lm) + 
    theme1
}

fscatter(df,"age","height")

ggsave("plot.pdf", width = 10, height = 10)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.