使用 qplot 的多层平滑图

multiple layer smoothed plots using qplot

这是我试过的情节的样本数据集

x<-runif(3, min=4, max=50)
y<-runif(6, min=3, max=14)

x1 <-runif(8, min=7, max=52)
y1 <-runif(5, min=5, max=18)

我可以使用以下代码绘制平滑线。

qplot(y,x, geom='smooth', span =0.05)
qplot(y1, x1, geom='smooth', span =0.05)

但它们被绘制在两个单独的图中;我怎样才能在不同图层的同一地块上绘制两条平滑线?

正如评论中指出的那样,您的示例存在一些问题

set.seed(1)
x <- sort(runif(20, min=4, max=50))
y <- sort(runif(20, min=3, max=14))

x1 <-sort(runif(20, min=7, max=52))
y1 <-sort(runif(20, min=5, max=18))

您可以使用 qplot 并将一堆层串在一起

library(ggplot2)
qplot(x, y) + geom_smooth(aes(x, y)) + geom_point(aes(x1, y1)) + geom_smooth(aes(x1, y1))

但是一旦数据格式正确

,使用起来会更容易ggplot
dd <- data.frame(x, x1, y, y1)
ll <- reshape(dd, dir = 'long', varying = list(1:2, 3:4))

ggplot(ll, aes(x, y, group = time)) + geom_point() + geom_smooth()