0

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")
2
  • Eventually temp[[plot_var_y]] Commented Dec 12, 2017 at 9:05
  • Thanks. How about inside ggplot function? Commented Dec 12, 2017 at 9:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.