2016-06-27 86 views
0

我是R新手。我有一個問題困擾了我好幾天。 我有3組數據。3分散點組的一個散點圖

set.seed(1) 
x1 = runif(30, 0, 7) 
y1 = runif(30, 0, 7) 
x2 = runif(30, 10, 20) 
y2 = runif(30, 10, 20) 
x3 = runif(30, 0, 7) 
y3 = runif(30, 10, 20) 

我想知道如何在同一個x-y圖形和不同顏色上繪製這3組散點圖?與基礎R

回答

0
xy <- data.frame(X = c(x1,x2,x3), 
      Y = c(y1,y2,y3), 
      Group = factor(rep(1:3, each = 30))) 

library(ggplot2) 
ggplot(xy, aes(x = X, y = Y)) + geom_point(aes(colour = Group)) 
0

一種解決方案可以是:

plot(x1, y1, xlim = c(0,20), ylim = c(0,20)) 
lines(x2, y2, type = "p", col = "red") 
lines(x3, y3, type = "p", col = "green")