【问题标题】:Adding Summary Statistics to a graph using the annotation feature : ggplot2使用注释功能将汇总统计信息添加到图形中:ggplot2
【发布时间】:2020-08-23 23:43:21
【问题描述】:

我有兴趣为 iris 数据制作类似的绘图,并在绘图上生成汇总统计数据: https://imgur.com/a/GasBB8r

我在这里关注这个帖子:How to add summary statistics in histogram plot using ggplot2?

df <- iris
df.m <- melt(df, id="Species")

#Calculating the summary statistics
summ <- df.m %>% 
  group_by(variable) %>% 
  summarize(min = min(value), max = max(value), 
            mean = mean(value), q1= quantile(value, probs = 0.25), 
            median = median(value), q3= quantile(value, probs = 0.75),
            sd = sd(value))

然后我修改了代码以制作密度图而不是直方图:

p1 <- ggplot(df.m) + geom_density(aes(x = value), fill = "grey", color = "black") + 
    facet_wrap(~variable, scales="free", ncol = 2)+ theme_bw()

我这里好像有问题:

p1+geom_density(data=summ,label =split(summ,summ$variable),
npcx = 0.00, npcy = 1, hjust = 0, vjust = 1,size=2)

有谁知道问题出在哪里?另外,是否可以仅使用 ggplot2 来完成此操作?我正在使用没有管理员权限的计算机来下载许多库(我有 reshape2、dplyr、ggplot2)。这应该使用 ggplot2 中的 annotate() 函数来完成吗?有没有办法将每个图形的 x 轴更改为“log”?

【问题讨论】:

    标签: r ggplot2 plot graph summary


    【解决方案1】:

    我会建议下一种方法,因为您只有几个包。您可以将摘要添加为文本注释,但您应该围绕每个组的文本位置进行调整。如果您在aes() 中申请ggplot(),则log() 转换也是可能的。我将向您展示两种注释方法。

    library(ggplot2)
    library(dplyr)
    
    #Data
    df <- iris
    df.m <- melt(df, id="Species")
    

    在这里,我们创建注释:

    #Calculating the summary statistics and create the label
    summ <- df.m %>% 
      group_by(variable) %>% 
      summarize(min = min(value), max = max(value), 
                mean = mean(value), q1= quantile(value, probs = 0.25), 
                median = median(value), q3= quantile(value, probs = 0.75),
                sd = sd(value)) %>%
      mutate_if(is.numeric, round, digits=2) %>%
      mutate(lab = paste("min = ", min, "\nmax = ", max, "\nmean = ", mean, 
                        "\nq1 = ", q1, "\nmedian = ", median, "\nq3 = ", q3, "\nsd = ", sd),
             position=c(1.5, 0.8, 0.25, -2)) %>% select(variable, lab, position)
    

    如果您想定义标签的位置,您必须修改上一节中确定 x 位置的 position 变量。接下来是使用该情节的代码:

    #Plot
    p1 <- ggplot(df.m) + geom_density(aes(x = log(value)), fill = "grey", color = "black") + 
      facet_wrap(~variable, scales="free", ncol = 2)+ theme_bw()
    p1 <- p1 + geom_text(data = summ, aes(x=position, label = lab), y=Inf, hjust=1, vjust=1.2, size=3)
    p1
    

    输出:

    注解的 x 位置在 summ 中定义。如果你想避免它,你只需使用下一个代码:

    p1 <- ggplot(df.m) + geom_density(aes(x = log(value)), fill = "grey", color = "black") + 
      facet_wrap(~variable, scales="free", ncol = 2) + theme_bw()
    p1 <- p1 + geom_text(data = summ, aes(label = lab), x = Inf, y = Inf, hjust = 1, vjust = 1.2, size = 3)
    p1
    

    输出:

    您可以选择这些选项中的任何一个。你申请的功能不起作用的原因可能是gridgridExtra包。

    【讨论】:

    • 感谢您的回复!我有 gridExtra 包,你建议我在这种情况下使用它吗?
    • @stats555 有时使用该包很棘手,因为您必须创建 grobs,然后在内部甚至修改 ggplot 对象。我还检查了您尝试使用的功能属于包ggpmisc。正如我所提到的,我提出了一种方法。你必须做出决定。让我知道这是怎么回事:)
    • @Duck - 这非常聪明!被低估了。
    • @Duck 很好的答案。与其多次使用round,不如使用`mutate_if(is.numeric, round, digits=2)。我有一个问题,我怎样才能让geom_text 左对齐?
    • @Duck 你能访问this question吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    相关资源
    最近更新 更多