【问题标题】:Combining geom_vridgeline with geom_line in one plot将 geom_vridgeline 与 geom_line 组合在一个图中
【发布时间】:2020-02-05 15:40:35
【问题描述】:

我想将 geom_vridgeline 与(连接的)折线图结合起来。

geom_line(和 geom_point)的值来自不同的数据集。 IE。这不是 geom_vridgeline 中使用的观察值的时刻(平均值、中值等)。

我尝试了简单的 geom_vridgeline(...) + geom_line(data...)+geom_point(..) 以及一些调整(添加第二个 x 轴等)。

library(tidyverse)
library(ggridges)

#The original chart
ggplot(iris, aes(x=Species, y=Sepal.Width, width = ..density..,     fill=Species)) +
geom_vridgeline(stat="ydensity", trim=FALSE, alpha = 0.85, scale = 0.25)

# another dataset
df<-iris%>%
group_by(Species)%>%
summarise(newvar=mean(Sepal.Width))

# add line from new dataset (doesn't work)
ggplot(iris, aes(x=Species, y=Sepal.Width, width = ..density..,     fill=Species)) +
geom_vridgeline(stat="ydensity", trim=FALSE, alpha = 0.85, scale = 0.25)+
geom_line(data=df,aes(x=factor(Species),y=newvar))

类似的东西

https://www.dropbox.com/s/1v7ycl23014hau8/Rplot.png?dl=1

注意:在我的真实示例中,x 轴实际上是连续的。我假设一旦我弄清楚如何添加 geom_line 我也可以添加 geom_point

非常感谢!

【问题讨论】:

    标签: r ggplot2 ggridges ridgeline-plot


    【解决方案1】:
     ggplot() + 
        geom_line(data=df,mapping=aes(x=Species,y=newvar,group = 1)) +
        geom_vridgeline(data=iris, aes(x=Species, y=Sepal.Width, width = ..density..,     fill=Species),stat="ydensity", trim=FALSE, alpha = 0.85, scale = 0.25)
    

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 2015-06-30
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 2020-06-28
      • 2017-11-28
      相关资源
      最近更新 更多