0

As part of a more complicated issue, I am trying to grab a string from a list that matches a variable I want to plot.

# http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/
set.seed(1234)
df <- data.frame(cond = factor( rep(c("A","B"), each=200) ), 
                 rating = c(rnorm(200),rnorm(200, mean=.8)))
library(ggplot2)
myList <- list(something=c("rating", "foo1"),
               else=c("foo2", "foo3"))
myList[[1]][1] # [1] "rating"

# does not work
  ggplot(df, aes(x=myList[[1]][1])) + geom_histogram(binwidth=.5)
# works
  ggplot(df, aes(x=rating)) + geom_histogram(binwidth=.5)

I want to get rating from my list rather than type it directly. Any ideas how to achieve this?

Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • 1
    try `aes_string` in the first ggplot call instead of `aes` – NicE Feb 22 '15 at 07:48
  • I just closed it so it doesn't persist as an unanswered question and used a pointer to another question with a somewhat richer application of hte aes_string capacities as the "duplicate". – IRTFM Feb 22 '15 at 18:43
  • I'd be happy to add @NicE's comment as an answer. I did not come across the duplicate in my search because I did not know about `aes_string`. – Eric Green Feb 23 '15 at 01:38

0 Answers0