ggplot2中的小提琴图的中位数和四分位数 [英] Median and quartile on violin plots in ggplot2

查看:2357
本文介绍了ggplot2中的小提琴图的中位数和四分位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot2绘制一些小提琴图,但我注意到中位数,第一和第三四分位数不会自动显示。我相信这些信息会对这些情节提供更多信息。有人知道一种方法吗?

I would like to draw some violin plots with ggplot2, but I noticed that median and first and third quartile are not automatically displayed. I believe these plots would be much more informative with this information. Does anybody know of a way to do it?

推荐答案

我从谷歌搜索中发现了这个:

I discovered this from a google search:

首先,这个堆栈溢出文章表示您可以添加 stat_summary(fun.y =median,geom =point)以绘制小提琴曲线上的中位数作为一个点。

First, this Stack Overflow post indicates that you can add stat_summary(fun.y="median", geom="point") to plot the median on a violin plot as a point.

关于四分位数,您可能必须为上面的fun.y参数编写自己的函数,如这里。例如:

With regard to quartiles, you will likely have to write your own function for the fun.y argument above, as demonstrated on here. E.g.:

median.quartile <- function(x){
    out <- quantile(x, probs = c(0.25,0.5,0.75))
    names(out) <- c("ymin","y","ymax")
    return(out) 
}

完整的代码可能如下所示:

The full code might look like this:

require(ggplot2)

median.quartile <- function(x){
  out <- quantile(x, probs = c(0.25,0.5,0.75))
  names(out) <- c("ymin","y","ymax")
  return(out) 
}

ggplot(iris,aes(Species,Sepal.Length))+
  geom_violin()+
  stat_summary(fun.y=median.quartile,geom='point')

这篇关于ggplot2中的小提琴图的中位数和四分位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆