0

I want to be able to assign the variable var outside of my ggplot call, and use it to reference a column of my dataframe within the ggplot call.

The following will graph the x variable of my df:

df =  data.frame(x=1:10,y=1:10)

df %>%
ggplot(aes(x)) + 
  geom_histogram()
# Outputs Desired Plot

The following is similar to above, but does not work, as "x" does not represent the column x.

var = "x"

df %>%
  ggplot(aes(var)) + 
  geom_histogram()
# Outputs error message

How do I assign var so that it can be used to represent x in the same way as the first call?

0 Answers0