我想为数据框创建一个图表,其列名包含特殊字符。请考虑以下示例:
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"))
最后一个命令的输出是:
Error in eval(expr, envir, enclos) : object 'bar' not found
有人知道创建这个情节的方法吗?或者这只是limitation of ggplot?
答案 0 :(得分:11)
你应该添加反引号``像这样:
ggplot(f) + geom_line(aes_string(x="foo", y="`bar->baz`"))
或
ggplot(f) + geom_line(aes(x=foo, y=`bar->baz`))
答案 1 :(得分:0)
param&lt; - &#39; TEST +&#39;
testParam&lt; - 粘贴(&#39; ',param,'
&#39;,sep =&#34;&#34;)
plot1&lt; - ggplot(dataset,aes_string(x = testParam))