2013-10-25 48 views
5

我想創建一個數據框的情節,它的列名包含特殊字符。請看下面的例子:ggplot2:如何繪製包含特殊字符的列?

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

回答

7

您應該添加反引號``這樣的:

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

或者

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

PARAM < - 'TEST +'

testParam < - 粘貼( '',param,'' 09月=」 「)

plot1 < - ggplot(數據集,aes_string(x = testParam))