【问题标题】:R: ggplot2, how to annotate summary statistics on each panel of a panel plotR:ggplot2,如何在面板图的每个面板上注释汇总统计信息
【发布时间】:2015-05-28 01:02:08
【问题描述】:

如何使用 R 中的 ggplot2 在以下绘图的每个面板中添加标准差的文本注释(例如 sd = sd_value)?

library(datasets)
data(mtcars)
ggplot(data = mtcars, aes(x = hp)) + 
        geom_dotplot(binwidth = 1) + 
        geom_density() + 
        facet_grid(. ~ cyl) + 
        theme_bw()

我会发布情节的图片,但我没有足够的代表。

我认为“geom_text”或“annotate”可能有用,但我不太确定如何使用。

【问题讨论】:

标签: r ggplot2


【解决方案1】:

如果您想改变每个方面的文本标签,您将需要使用geom_text。如果您希望在每个构面中出现相同的文本,您可以使用annotate

p <- ggplot(data = mtcars, aes(x = hp)) + 
  geom_dotplot(binwidth = 1) + 
  geom_density() + 
  facet_grid(. ~ cyl)

mylabels <- data.frame(cyl = c(4, 6, 8), 
                       label = c("first label", "seond label different", "and another"))

p + geom_text(x = 200, y = 0.75, aes(label = label), data = my labels)

### compare that to this way with annotate

p + annotate("text", x = 200, y = 0.75, label = "same label everywhere")

现在,如果您真的希望在此示例中使用 cyl 的标准差,我可能会先使用 dplyr 进行计算,然后使用 geom_text 完成此计算,如下所示:

library(ggplot2)
library(dplyr)

df.sd.hp <- mtcars %>%
  group_by(cyl) %>%
  summarise(hp.sd = round(sd(hp), 2))

ggplot(data = mtcars, aes(x = hp)) + 
  geom_dotplot(binwidth = 1) + 
  geom_density() + 
  facet_grid(. ~ cyl) +
  geom_text(x = 200, y = 0.75, 
            aes(label = paste0("SD: ", hp.sd)), 
            data = df.sd.hp)

【讨论】:

  • 谢谢。 geom_text 的最后一个例子正是我想要的。我还在习惯 ggplot2;有没有办法对 ggplot2 中的 stat_ 系列函数做同样的事情来进行常见的统计计算,而不必先使用 dplyr?
  • 另外,如何在标签的文本部分包含希腊字母(例如 sigma)和/或乳胶(例如 \sigma^2)?
  • 您可以使用 expression() 作为数学符号。
  • 在 geom_text 中使用 parse=T 为我解决了这个问题。所以对于上面的例子,以下工作: geom_text(x = 200, y = 0.75, aes(label = paste0("sigma ==", hp.sd)), data = df.sd.hp, parse=T)
  • 现在,如何在标签中包含多行以包括平均值和标准差?
【解决方案2】:

我更喜欢统计数据出现在构面标签本身中时的图表外观。我制作了以下脚本,它允许选择显示标准差均值计数。本质上,它会计算汇总统计信息,然后将其与名称合并,以便您具有 CATEGORY (SUMMARY STAT = VALUE) 格式。

   #' Function will update the name with the statistic of your choice
AddNameStat <- function(df, category, count_col, stat = c("sd","mean","count"), dp= 0){

  # Create temporary data frame for analysis
  temp <- data.frame(ref = df[[category]], comp = df[[count_col]])

  # Aggregate the variables and calculate statistics
  agg_stats <- plyr::ddply(temp, .(ref), summarize,
                           sd = sd(comp),
                           mean = mean(comp),
                           count = length(comp))

  # Dictionary used to replace stat name with correct symbol for plot
  labelName <- mapvalues(stat, from=c("sd","mean","count"), to=c("\u03C3", "x", "n"))

  # Updates the name based on the selected variable
  agg_stats$join <- paste0(agg_stats$ref, " \n (", labelName," = ",
                           round(agg_stats[[stat]], dp), ")")

  # Map the names
  name_map <- setNames(agg_stats$join, as.factor(agg_stats$ref))
  return(name_map[as.character(df[[category]])])
}

将此脚本用于您的原始问题:

library(datasets)
data(mtcars)

# Update the variable name
mtcars$cyl  <- AddNameStat(mtcars, "cyl", "hp", stat = "sd")

ggplot(data = mtcars, aes(x = hp)) + 
  geom_dotplot(binwidth = 1) + 
  geom_density() + 
  facet_grid(. ~ cyl) + 
  theme_bw()

脚本应该易于更改以包含其他汇总统计信息。我也确信它可以被部分重写以使其更清洁!

【讨论】:

  • 格式不错;我喜欢。改进可能包括添加单位和允许多个统计信息。
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 2019-03-19
  • 2017-07-01
  • 2015-09-22
相关资源
最近更新 更多