Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: how to plot columns containing special characters?

Tags:

r

ggplot2

I'd like to create a plot for a data frame, the column names of which contain special characters. Consider the following example:

f <- data.frame(foo=c(1, 2, 3), bar=c(4, 5, 6))
# The following line works fine
ggplot(f) + geom_point(aes_string(x="foo", y="bar"))
names(f) <- c("foo", "bar->baz")
# The following also works, but seems not elegant
ggplot(f) + geom_line(aes(x=foo, y=f[,"bar->baz"])) 
# I'd like something like the following, but this doesn't work.
ggplot(f) + geom_line(aes_string(x="foo", y="bar->baz"))

The output of the last command is:

Error in eval(expr, envir, enclos) : object 'bar' not found

Does anybody know a way of creating this plot? Or is this simply a limitation of ggplot?

like image 712
Sjlver Avatar asked Oct 25 '13 09:10

Sjlver


1 Answers

You should add backquotes `` like this:

ggplot(f) + geom_line(aes_string(x="foo", y="`bar->baz`"))

Or

ggplot(f) + geom_line(aes(x=foo, y=`bar->baz`))
like image 64
agstudy Avatar answered Oct 21 '22 12:10

agstudy