10

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?

2 Answers 2

14

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`))
0

param <- 'TEST+'

testParam <- paste('',param,'',sep="")

plot1 <- ggplot(dataset, aes_string(x = testParam ))

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.