繁体   English   中英

使用ggplot2在同一图上有多个样条线?

[英]Multiple splines on the same plot with ggplot2?

我想在下面的图中添加其他样条线,但是如果我这样做, geom_point()将仅在第一个样条线上显示点。

n <- 10
d <- data.frame(x = 1:n, y = rnorm(n))
ggplot(d,aes(x,y)) + geom_point() + 
  geom_line(data=data.frame(spline(d, n=n*10)))

在此处输入图片说明

我如何在第二行上也显示点?

假设您最初在两个data.frames中拥有所需的数据,则此代码将起作用。 使用ggplot之前先制作样条线。

n <- 10
d <- data.frame(x = 1:n, y = rnorm(n))
d2 <- data.frame(x = 1:n, y = rnorm(n))
dd <- rbind(cbind(d, case = "d"), cbind(d2, case = "d2"))
ddsmooth <- plyr::ddply(dd, .(case), function(k) as.data.frame(spline(k, n = n * 10)))
ggplot(dd,aes(x, y, group = case)) + geom_point() + 
  geom_line(aes(x, y, group  = case), data = ddsmooth)

如果您的数据位于data.frame的不同列中,请使用reshape2 :: melt处理它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM