0

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

geom_line(和 geom_point)的值来自不同的数据集。即它不是 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

非常感谢!

4

1 回答 1

0
 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)
于 2019-10-08T11:11:38.903 回答