小提琴剧情与箱形剧情相结合 [英] Combining violin plot with box plot
本文介绍了小提琴剧情与箱形剧情相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的数据集(2组,每组n = 15)。使用ggplot2,我可以轻松地绘制出两组的小提琴情节或盒子情节。但是,我想绘制一个小提琴图,但是填充阴影对应于我的数据的三个四分位数。
有一个例子在SAS 这里,但我想在R中这样做。
解决方案
像这样使用 ggplot_build()
来获取轮廓?
编辑已更新显示原始数据的数量
require(ggplot2)#for ggplot
require(dplyr)#for mutation
df< -data.frame(#make sample data
grp = rep(1:6 ,每个= 15),
val = c(rnorm(15,runif(1)* 5,runif(1)),
rnorm(15,runif(1)* 5,runif(1) ),
rnorm(15,runif(1)* 5,runif(1)),
rnorm(15,runif(1)* 5,runif(1)),
rnorm 15,runif(1)* 5,runif(1)),
rnorm(15,runif(1)* 5,runif(1))
)
)
g< -ggplot(d f)+ geom_violin(aes(x = grp,y = val,group = grp),color =darkred,fill =darkred,size = 2)#build the base violins
coords< ; -ggplot_build(g)$ data#使用ggbuild获取大纲共同目标
d< -coords [[1]]#这将以可用的形式获得df $ b $组<-unique(d $ group $)#获取唯一的小提琴id
#函数创建geom_ploygon调用
fill_viol <-function(v,gr){
quants< -mutate(v, xl = x-violinwidth / 2,xr = x + violinwidth / 2,cuts = cut(y,quantile(df [df $ grp == gr,val])))#每个方向添加1/2宽度x value
plotquants< -data.frame(x = c(quants $ xl,rev(quants $ xr)),#left x bottom to top,then right x top to bottom
y = c(quants $ y,rev(quants $ y)),#将y值加倍以匹配
id = c(quants $ cuts,rev(quants $ cuts)))#按分位数截断以创建多边形ID
geom_polygon(aes(x,y,fill = as.factor(id)),data = plotquants)#return the geom_ploy gon object
g +#plot g
lapply(groups,function(x)fill_viol(d [d $ group == x,],x))+#plus每个小提琴的多边形对象
scale_fill_brewer(palette =Reds,#plus fill
name =Quantile \\\
,
labels = c(25,50, 75,100))+
coord_flip()
I have a very simple dataset (2 groups, n=15 per group). Using ggplot2, I can easily plot a violin plot or a box plot of the two groups. However, I would like to graph a violin plot, but have the fill shade correspond to the 3 quartiles of my data.
There is an example done in SAS here but I would like to do this in R.
解决方案
Like this using ggplot_build()
to get the outlines?
EDIT UPDATED TO SHOW QUANTILE OF THE ORIGINAL DATA
require(ggplot2) # for ggplot
require(dplyr) # for mutation
df<-data.frame( # make sample data
grp=rep(1:6,each=15),
val=c(rnorm(15,runif(1)*5,runif(1)),
rnorm(15,runif(1)*5,runif(1)),
rnorm(15,runif(1)*5,runif(1)),
rnorm(15,runif(1)*5,runif(1)),
rnorm(15,runif(1)*5,runif(1)),
rnorm(15,runif(1)*5,runif(1))
)
)
g<-ggplot(df)+geom_violin(aes(x=grp,y=val,group=grp),color="darkred",fill="darkred",size=2) # build the base violins
coords<-ggplot_build(g)$data # use ggbuild to get the outline co-ords
d<-coords[[1]] # this gets the df in a usable form
groups<-unique(d$group) # get the unique "violin" ids
# function to create geom_ploygon calls
fill_viol<-function(v,gr){
quants<-mutate(v,x.l=x-violinwidth/2,x.r=x+violinwidth/2,cuts=cut(y,quantile(df[df$grp==gr,"val"]))) # add 1/2 width each way to each x value
plotquants<-data.frame(x=c(quants$x.l,rev(quants$x.r)), # left x bottom to top, then right x top to bottom
y=c(quants$y,rev(quants$y)), # double up the y values to match
id=c(quants$cuts,rev(quants$cuts)))# cut by quantile to create polygon id
geom_polygon(aes(x,y,fill=as.factor(id)),data=plotquants) # return the geom_ploygon object
}
g + # plot g
lapply(groups,function(x)fill_viol(d[d$group==x,],x)) + # plus polygon objects for each violin
scale_fill_brewer(palette="Reds", # plus fill
name="Quantile\n",
labels=c("25","50","75","100")) +
coord_flip()
这篇关于小提琴剧情与箱形剧情相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文