R; replace variable with contents of the variable

How can I replace the variable by its content/value? See sample code below.

###########SET my plotting variable##########################
    plot_var_y<-'var1'
    plot_var_x<-'var2'
####################EVALUATE THE FOLLOWING##################
        temp$plot_var_y
        # The above expression evaluates to null which is not what I want.temp$var1 is what I was looking for

    plot1<-ggplot(aes(y = plot_var_y, x = as.factor(plot_var_x), fill = no_of_heads), data =temp) +
      geom_boxplot()+stat_summary(fun.y=mean, geom="line",aes(group=1),colour=I("red"))+stat_summary(fun.y=mean, geom="point",shape=18,size=3)+xlab("position")

Upvotes: 0

Views: 81

Answers (1)

axiom
axiom

Reputation: 408

Use aes_string instead of aes

try

 plot1<-ggplot(aes_string(y = plot_var_y, x = as.factor(plot_var_x), fill = no_of_heads), data =temp) +
      geom_boxplot()+stat_summary(fun.y=mean, geom="line",aes(group=1),colour=I("red"))+stat_summary(fun.y=mean, geom="point",shape=18,size=3)+xlab("position")

See How to use a variable to specify column name in ggplot

Upvotes: 1

Related Questions