Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

r - Plot multiple datasets with ggplot

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.

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) 

or equivalently:

qplot(x,y,data=A)  +geom_point(data=B,colour='red') + xlim(0, 10) 

If you want to plot to figures side by side, see ?par and look for the descriptions of 'mfcol' and 'mfrow'

In addition to ggsave, see ?pdf.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...