Help understand the error in a function I defined in R

前端 未结 2 1417
逝去的感伤
逝去的感伤 2020-12-11 18:45

I am very new to R and just learnt to write simple functions. Can someone help me understand why the following function does not work.

fboxplot <- functio         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 19:25

    The problem is that you are passing through varx and vary vectors, while the aes function expects variable names (not as strings, though). One way to fix this is to use the aes_string function to which you can pass variable names as strings (still not vectors, though):

    The following should work:

    fboxplot2 <- function(mydataframe, varx, vary)   {
         p <- ggplot(data=mydataframe, aes_string(x=varx, y=vary))
         p + geom_boxplot()   }
    
    fboxplot2(df, "col1", "col2") 
    

提交回复
热议问题