用ggplot绘制多个数据集 [英] Plot multiple datasets with ggplot
问题描述
我想在同一张图中绘制两组不同的点:A = [1 2; 3 4]
和B = [1 3; 2 4]
.我需要存储图,所以我的想法是先使用myPlot <- qplot
,然后使用ggsave
.
I want to plot, in the same graph, two different sets of points: A = [1 2; 3 4]
and B = [1 3; 2 4]
. I need to store the plot, so my idea is to use myPlot <- qplot
followed by ggsave
.
使用这种方法,如何在不出现错误formal argument "data" matched by multiple actual arguments
的情况下绘制多个数据集?
With such an approach, how can I plot multiple datasets without getting the error formal argument "data" matched by multiple actual arguments
?
这是我现在使用的代码:
Here is the code I am using now:
yPlot <- qplot(A[,1], A[,2], data = A[1:2], geom="point",
B[,1], B[,2], data = B[1:2], geom="point") + xlim(0, 10)
ggsave(filename="Plot.jpg", plot=myPlot, width = 12, height = 8)
推荐答案
以下是在同一图中绘制两个数据框的模板:
Here's a template for plotting two data frame in the same figure:
A = data.frame(x = rnorm(10),y=rnorm(10))
B = data.frame(x = rnorm(10),y=rnorm(10))
ggplot(A,aes(x,y)) +geom_point() +geom_point(data=B,colour='red') + xlim(0, 10)
或等效地:
qplot(x,y,data=A) +geom_point(data=B,colour='red') + xlim(0, 10)
如果要并排绘制图形,请参见?par
并查找"mfcol"和"mfrow"的描述
If you want to plot to figures side by side, see ?par
and look for the descriptions of 'mfcol' and 'mfrow'
除ggsave外,请参见?pdf
.
In addition to ggsave, see ?pdf
.
这篇关于用ggplot绘制多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!