r - 箱线图 mustache 上的样本大小

标签 r ggplot2 rstudio

我想在每个箱线图的 mustache 上显示“n = (n)”。我已经弄清楚如何使用 Fivenum 将这些标签放在每个盒子(q75)的顶部,但我无法让它们在晶须上方工作。 mustache 上方更好,因为我的地 block 非常困惑。

这里我使用 mtcars 重现了这些图 编辑:mtcars 没有显着的异常值,但我的数据集有。这就是为什么标签需要位于晶须顶部,而不仅仅是位于最高数据点。

旁注:我正在处理很多异常值,并希望将它们从显示中删除。 GGplot 可以做到这一点,但它仍然会在轴中包含异常值,这给了我一个非常“缩小”的图。我的解决方法已包含在内。我使用基本箱线图函数来计算最高的须线,并使用 coord_cartesian 将上限设置为略高于该上限。

> data("mtcars")
> head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
> 
> d = data.table(mtcars)
> 
> give.n <- function(x){
+   return(data.frame(y = fivenum(x)[4],
+                     label = paste("n =",length(x))))
+ }
> 
> p1 <- boxplot(mpg~cyl, data=mtcars, outline=FALSE,
+               plot=0)
> p1stats <- p1$stats[5,]
> head(p1stats)
[1] 33.9 21.4 19.2
> upperlim <- max(p1$stats, na.rm = TRUE) * 1.05
>   
> p <- ggplot(d, aes(x=factor(cyl), y=mpg)) +
+     geom_boxplot() +
+ stat_summary(fun.data = give.n, geom = "text", vjust=-.5)
> 
> p <- p + coord_cartesian(ylim = c(0, upperlim))

我尝试更改此功能(有效):

> give.n <- function(x){
+   return(data.frame(y = fivenum(x)[4],
+                     label = paste("n =",length(x))))
+ }

为此,使用 p1 统计数据的第 5 行(上面的 mustache ):

give.n <- function(x){
  return(data.frame(y = p1stats,
                    label = paste("n =",length(x))))
}

但这会返回: bad plot

如何才能仅在每个盒子的正确 mustache 点上显示标签?

PS - 抱歉,我不熟悉在这里发帖,但我尝试过

最佳答案

这是一个带有 dpylr 的 ggplot 解决方案:

ggplot(mtcars, aes(x=cyl, y=mpg, group=cyl)) + 
  geom_boxplot() + 
  geom_text(data=mtcars %>% group_by(cyl) %>% summarise(top = max(mpg), n=n()), aes(x=cyl, y=top, label= paste0("n = ", n)), nudge_y=1)

enter image description here

编辑

可能有一种更简洁的方法,但我认为这可行。为了强调,我编辑了 cyl=8 的数据点:

 ggplot(mtcars, aes(x=cyl, y=mpg, group=cyl)) + 
  geom_boxplot() + 
  geom_text(data=mtcars %>% 
              group_by(cyl) %>% 
              summarise(q3 = quantile(mpg, 0.75),
                        q1 = quantile(mpg, 0.25),
                        iqr = q3 - q1,
                        top = min(q3 + 1.5*iqr, max(mpg)), 
                        n=n()), 
            aes(x=cyl, y=top, label= paste0("n = ", n)), nudge_y=1)

enter image description here

关于r - 箱线图 mustache 上的样本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56240613/

相关文章:

r - 具有常见图例的 ggarrange 在 Markdown 中产生额外的空白图

r - 如何在ggplot渐变颜色比例尺中包含更多小数?

r - 使用包 texreg 和 RStudio/rmarkdown/knitr 创建模型汇总表的问题

r - 为什么使用 Ctrl-Shift-F10 重新启动 R 不会清除我的环境变量?

不加载包的R函数调用

r - 为Ax = b并行求解Solve()吗?

r - 将数据分成三组并平衡数据

r - 如何使用R对大型数据集有效地检查点是否在多边形中?

r - 在 Shiny 的应用程序中插入dygraph时出现问题

r - 使用 R Markdown/Notebook 缓存 SQL block ,无需在 RStudio 中进行编织