【问题标题】:multiple layer smoothed plots using qplot使用 qplot 的多层平滑图
【发布时间】:2016-03-13 09:17:56
【问题描述】:

这是我尝试过的绘图的样本数据集

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)

但它们被绘制在两个单独的图中;如何在不同图层的同一图上绘制两条平滑线?

【问题讨论】:

  • 这样的? ggplot()+geom_smooth(aes(x,y))+geom_smooth(aes(x1,y1))
  • 您示例中的向量具有不同的长度。如果您想在geom_smooth() 中组合它们,它们应该是相同的大小。

标签: r ggplot2 regression dplyr ggvis


【解决方案1】:

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

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()

【讨论】:

  • 嘿,我绘制成功了;但是有什么办法可以删除点并在情节上留下线条?
  • @saurabh 只需删除点层ggplot(ll, aes(x, y, group = time)) + geom_smooth()
猜你喜欢
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多