Sjlver
Sjlver

Reputation: 1295

ggplot2: how to plot columns containing special characters?

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?

Upvotes: 11

Views: 2824

Answers (2)

Mark
Mark

Reputation: 161

param <- 'TEST+'

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

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

Upvotes: 0

agstudy
agstudy

Reputation: 121588

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

Upvotes: 14

Related Questions