叠加小提琴图ggplot2

编程入门 行业动态 更新时间:2024-10-10 06:18:31
本文介绍了叠加小提琴图ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

library(ggplot2)#生成一些数据 coco1< -rnorm(10000,0,1) coco2< -c(runif(10000)) decile< -rbinom(10000,9 ,1/2)+1 椰子< -data.frame(coco1,coco2,decile) #提取coco1系列的小提琴曲线p< - ggplot( (aes(alpha = 0.3,color =#1268FF))p # (椰子,aes(因子(十分位),coco2)) q <-q + geom_violin(aes(alpha = 0.3,color =# 3268FF))q

我想绘制小提琴情节p和 q,在同一张图上,我希望每个q的小提琴曲线都要超过相应的小提琴曲线p。

解决方案

您可以将第二个图的 geom_violin 添加到您的第一个图中:

(ces,aes(factor(decile),coco1))p <-p + geom_violin(aes(color =#1268FF),alpha = 0.3)q <-p + geom_violin(aes(y = coco2,color =#3268FF),alpha = 0.3)

现在, q 包含两个小提琴的版本。

通过方式:如果你想摆脱颜色图例,你必须在 aes 之外指定颜色。

I would like to plot two series of ten violin plots one over the second :

library(ggplot2) #generate some data coco1<-rnorm(10000,0,1) coco2<-c(runif(10000)) decile<-rbinom(10000,9,1/2)+1 coconut<-data.frame(coco1,coco2,decile) #draw the violin plots of the coco1 serie p <- ggplot(coconut, aes(factor(decile), coco1)) p<-p + geom_violin(aes(alpha=0.3,colour="#1268FF")) p #draw the violin plots of the coco2 serie q <- ggplot(coconut, aes(factor(decile), coco2)) q<-q + geom_violin(aes(alpha=0.3,colour="#3268FF")) q

I would like to plot the violin plot "p" and "q", on the same graph, and I want each violin plot of "q" to be over the corresponding violin plot of "p".

解决方案

You can just add the geom_violin of the second plot to your first one:

p <- ggplot(coconut, aes(factor(decile), coco1)) p <- p + geom_violin(aes(colour = "#1268FF"), alpha = 0.3) q <- p + geom_violin(aes(y = coco2, colour = "#3268FF"), alpha = 0.3)

Now, q contains both versions of the violins.

By the way: If you want to get rid of the colour legend, you have to specify the colour outside of aes.

更多推荐

叠加小提琴图ggplot2

本文发布于:2023-07-18 08:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142774.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小提琴

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!