R 将两个图合并为一个图
我试着用两个数据集做一个图来比较它们的值R 将两个图合并为一个图,r,ggplot2,R,Ggplot2,我试着用两个数据集做一个图来比较它们的值 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") 但当我运行此代码时,我得
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
有人知道我做错了什么吗?你显然需要进一步学习图形语法。
+
登录ggplot
不能仅用于将另一个ggplot添加到绘图中。您可以使用@ulfelder在评论中提到的facet_网格
,或grid。从gridExtra
包中排列
:
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)
require(ggplot2)
图书馆(gridExtra)
图书馆(网格)
dfpos如果您将两个数据集合并为一个,并创建一个标识源数据集的因子,那么这将更容易实现。然后,您可以使用facet.grid
制作二联图,或者使用fill=[factor id'ing source]
在单个绘图中比较它们。您希望如何组合这两个绘图?两个并排的图,还是一个并排带条的图?
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)