简体   繁体   English

如何使用 function 在 geom_boxplot 中放置文本

[英]How to use a function to place text in geom_boxplot

I'm working on some boxplots.我正在研究一些箱线图。 Here is a working example:这是一个工作示例:

data(mtcars)

# Compute means for each group
mpgmn <- aggregate(mpg ~ cyl, mtcars, mean)
mpgmn$mpg <- round(mpgmn$mpg, 2)

# Same thing for 50th and 75th %tiles
mpglims <- mtcars %>% group_by(cyl) %>% 
        summarize(q50 = quantile(mpg, probs = 0.50), 
                  q75 = quantile(mpg, probs = 0.75))

# Plot
library(ggplot2)
g <- ggplot(mtcars, aes(x = as.factor(cyl), y = mpg, 
     fill = as.factor(cyl)))
g <- g + geom_boxplot()
g <- g + stat_summary(fun = mean, color = "white", geom = "point", 
     shape = 18, size = 3, show.legend = FALSE)
g <- g + geom_text(data = mpgmn, 
     aes(label = paste("mean = ", mpg),
     y = mpg + 0.5), color = "white")
g

All of this works.所有这些都有效。 However, I would like to use the mpglims that I computed (that look correct to me) to place the white text inside each of the boxplots (ie, instead of the current vertical position argument: y = mpg + 0.05 ).但是,我想使用我计算的mpglims (对我来说看起来正确)将白色文本放置在每个箱线图中(即,而不是当前的垂直 position 参数: y = mpg + 0.05 )。 Is there a way to tell R to pick the halfway point between the two values that I computed for each group for the vertical position?有没有办法告诉 R 选择我为垂直 position 为每个组计算的两个值之间的中间点?

The easiest way might be to add one more variable to the creation of mpglims :最简单的方法可能是在创建mpglims时再添加一个变量:

mpglims <- mtcars %>% group_by(cyl) %>% 
        summarize(q50 = quantile(mpg, probs = 0.50), 
                  q75 = quantile(mpg, probs = 0.75),
                  mid = (q50 + q75)/ 2)

Use mid in y = mid in the geom_text() call.geom_text()调用中使用mid in y = mid

If you want to use what you calculated in the first data frame mpgmn , make it a bit easier on yourself and add that to mpglims , as well:如果您想使用您在第一个数据帧mpgmn中计算的内容,请让自己更轻松一些并将其添加到mpglims中,以及:

mpglims <- mtcars %>% group_by(cyl) %>% 
        summarize(q50 = quantile(mpg, probs = 0.50), 
                  q75 = quantile(mpg, probs = 0.75),
                  mid = (q50 + q75)/ 2,
                  mmpg = mean(mpg) %>% round(., digits = 2))

It creates the same thing as your aggregate() call.它创建与您的aggregate()调用相同的东西。 Check it out:一探究竟:

mpglims[, 5] %>% unlist()
# mmpg1 mmpg2 mmpg3 
# 26.66 19.74 15.10 

Putting all informations already provided by Kat (this answer should be the accepted one) and the OP, here is one possibly tidyverse way:将 Kat 已经提供的所有信息(这个答案应该是公认的)和 OP 提供,这是一种可能tidyverse方式:

library(tidyverse)

mtcars %>% 
  select(cyl, mpg) %>% 
  group_by(cyl = as.factor(cyl)) %>% 
  mutate(mpg_mean = round(mean(mpg, na.rm = TRUE),2)) %>% 
  mutate(q50 = quantile(mpg, probs = 0.50), 
         q75 = quantile(mpg, probs = 0.75)) %>% 
  mutate(mid = (q50 + q75)/ 2) %>%
  ggplot(aes(x = cyl,  y = mpg, fill = cyl)) +
  geom_boxplot() +
  stat_summary(fun = mean, color = "white", geom = "point", 
               shape = 18, size = 3, show.legend = FALSE) +
  geom_text(aes(label = paste("mean = ", mpg_mean),
                y = mid), color = "white")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM