How to use a variable to specify column name in ggplot

前端 未结 6 1468
别那么骄傲
别那么骄傲 2020-11-22 03:41

I have a ggplot command

ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) )

inside a function. But I would like to be a

6条回答
  •  日久生厌
    2020-11-22 04:34

    Here's an extremely simple way!

    Do these two things:

    1. Turn the column name into a symbol with sym()
    2. Add !! when you use it

    Minimal Reproducible Example:

    my_col <- sym("Petal.Length")
    
    iris %>% 
      ggplot(aes(x = Sepal.Length, y = !!my_col)) +
      geom_point()
    

提交回复
热议问题