堆叠的平整小提琴ggplot
我想绘制一个小提琴ggplot,其中x轴是年份,而y轴是价值。每年,我都需要三个小提琴。当我尝试这样做时,Y轴并未将其堆叠在一起,因此这三个的密度尚未清楚地显示。每年,我希望它们以相同的Y轴绘制,并且填充颜色是透明的,因此可以看到这三个。数据集很长(很难复制一个较短的数据集),但这是sum1.14
,sum5.14
和sum10.14的三个数据框
。
plot.density.tenor <- function(cent_list_sum1,cent_list_sum5,cent_list_sum10,start_year,end_year){
df1 <- melt(ldply(cent_list_sum1, data.frame))
df5 <- melt(ldply(cent_list_sum5, data.frame))
df10 <- melt(ldply(cent_list_sum10, data.frame))
df <- rbind(df1,df5,df10)
years <- seq(start_year,end_year)
df$year <- rep(rep(as.character(seq(start_year,end_year)),each=nrow(df1)/length(years)),3)
df$tenor <- rep(c("1","5","10"),each=nrow(df1))
df$tenor <- factor(df$tenor, levels=c("1", "5", "10"))
ggplot(df, aes(year, value, color = tenor)) +
geom_flat_violin() +
ggtitle("Centrality Measure Distribution") + ylab("Centrality") + xlab("Year") +
stat_summary(colour="red", size = 0.5)
#theme(legend.position = "none")
}
plot.density.tenor(sum1.14,sum5.14,sum10.14,2010,2021)
I want to plot a violin ggplot where the x-axis are the years and the y-axis is the value. In each year, I need to have three of the violin plots. When I try to do so, the y-axis are not stacked together, so the density of these three isn't shown clearly. In each year, I want them to be plotted in the same y-axis and the fill color be transparent so all three can be seen. The dataset is pretty long (it was pretty hard to replicate a shorter one) but it is three dataframes of sum1.14
, sum5.14
and sum10.14
.
plot.density.tenor <- function(cent_list_sum1,cent_list_sum5,cent_list_sum10,start_year,end_year){
df1 <- melt(ldply(cent_list_sum1, data.frame))
df5 <- melt(ldply(cent_list_sum5, data.frame))
df10 <- melt(ldply(cent_list_sum10, data.frame))
df <- rbind(df1,df5,df10)
years <- seq(start_year,end_year)
df$year <- rep(rep(as.character(seq(start_year,end_year)),each=nrow(df1)/length(years)),3)
df$tenor <- rep(c("1","5","10"),each=nrow(df1))
df$tenor <- factor(df$tenor, levels=c("1", "5", "10"))
ggplot(df, aes(year, value, color = tenor)) +
geom_flat_violin() +
ggtitle("Centrality Measure Distribution") + ylab("Centrality") + xlab("Year") +
stat_summary(colour="red", size = 0.5)
#theme(legend.position = "none")
}
plot.density.tenor(sum1.14,sum5.14,sum10.14,2010,2021)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以通过此假数据复制类似的内容:
我认为我们可以通过下面的代码获得类似的内容。首先,我们覆盖默认值以使每个绘图更宽(这可能需要对不同的数据进行调整),然后告诉“躲避”功能,每个连续的“男高音”组的基线之间应插入零空间。
I can replicate something similar with this fake data:
I think we can get something like what you're describing with the code below. First we override the defaults to make each plot wider (this will probably need adjustment for different data), then tell the "dodging" function that there should be zero space inserted between the baseline of each successive "tenor" group.