2017-05-08 134 views
0

我正在试图制作两个数据集的一个图来比较它们的值。将两个图组合成一个

ggplot() 
ggplot(pos_plot, aes(x=WORD, y=FREQ)) + 
    geom_bar(position="dodge", colour="blue", stat = "identity") + 
ggplot(neg_plot, aes(x=WORD, y=FREQ)) + 
    geom_bar(position="dodge", colour="red", stat = "identity") 

但是当我运行这段代码我得到的错误:

Error: Don't know how to add o to a plot 

有谁知道我做错了吗?

+1

这将是更容易,如果你合并做两个数据集合为一个并创建一个识别源数据集的因子。然后你可以使用'facet.grid'来形成一个diptych,或者使用'fill = [factor id'source']来将它们在一个绘图中进行比较。 – ulfelder

+0

你想如何结合这两个地块?两个并排的情节,或一个情节与并排的酒吧? –

回答

0

您显然需要更多地研究图形的语法。 ggplot中的+登录不能用于在您的图中添加另一个ggplot。你可以使用facet_grid由@ulfelder在评论中提到的,或grid.arrangegridExtra包:

require(ggplot2) 
library(gridExtra) 
library(grid) 

dfpos <- data.frame(x=1,y=1) 
dfneg <- data.frame(x=-1,y=-1) 

ppos <- ggplot(dfpos, aes(x,y)) + geom_point() 
pneg <- ggplot(dfneg, aes(x,y)) + geom_point() 

grid.arrange(ppos,pneg) 

或者重写自己的代码:

ppos <- ggplot(pos_plot, aes(x=WORD, y=FREQ)) + 
     geom_bar(position="dodge", colour="blue", stat = "identity") 
pneg <- ggplot(neg_plot, aes(x=WORD, y=FREQ)) + 
     geom_bar(position="dodge", colour="red", stat = "identity") 
grid.arrange(ppos,pneg)