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?
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`))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With