【问题标题】:ggplot2: add line and points showing means (stat_summary)ggplot2:添加线和点显示手段(stat_summary)
【发布时间】:2023-03-03 01:45:01
【问题描述】:

所以我正在使用这个数据框:

xym <- data.frame(
  Var1 = c("vloga", "odločitve", "dolgoročno", 
         "krizno", "uživa v", "vloga",   "odločitve", 
         "dolgoročno",  "krizno",   "uživa v", "vloga",
         "odločitve","dolgoročno", "krizno",   "uživa v",
         "vloga","odločitve",  "dolgoročno", "krizno",
         "uživa v"),
  Var2 = c("Nad","Nad", "Nad",  "Nad",  "Nad", "Pod",
         "Pod",  "Pod",  "Pod",  "Pod",  "Enak","Enak",
         "Enak", "Enak", "Enak", "Sam.", "Sam.", "Sam.",
         "Sam.", "Sam."),
  value = c(4, 3, 4, 4, 3, 3, 3, 2, 3, 3, 3, 2.5, 2.5,
             2, 3.5 ,5 ,6 ,6 ,5 ,6))

使用此代码:

p <- ggplot(xym, aes(x = Var1, y = value, fill = Var2)) + coord_flip()+
  theme_bw() + scale_fill_manual(values = c("yellow", "deepskyblue1", "yellowgreen","orchid4")) + xlim(rev(levels(xym$Var1)))+ theme(axis.title=element_blank(),axis.ticks.y=element_blank(),legend.position = "bottom",
                                                                                                                                     axis.text.x = element_text(angle = 0,vjust = 0.4)) +
  geom_bar(stat = "identity", width = 0.7, position = position_dodge(width=0.7)) +
  geom_text(aes(x = Var1, y =max(value), label = round(value, 2), fill = Var2), 
            angle = 0, position = position_dodge(width = 0.7), size = 4.2)
p + labs(fill="")

p +  stat_summary(fun.y=mean, colour="red", geom="line", aes(group = 1))

我产生输出:

但在 红线 旁边是按问题标记的总平均值(即“dolgoročno”、“krizno”等),我想加分和旁边各个问题组的条形图和标签均值

我的输出应该类似于下图,(我是在油漆中完成的),其中黑点代表我想要的点,第一个点的值 3.6 是 (6,2,4,2.5) 的平均值并代表我想要的价值标签。

我也看过:

Plot average line in a facet_wrap

ggplot2: line connecting the means of grouped data

How to label graph with the mean of the values using ggplot2

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一个选项如下。我按照您的代码添加了几行代码。

    # Your code
    p <- ggplot(xym, aes(x = Var1, y = value, fill = Var2)) +
         coord_flip() +
         theme_bw() +
         scale_fill_manual(values = c("yellow", "deepskyblue1", "yellowgreen","orchid4")) +
         xlim(rev(levels(xym$Var1))) +
         theme(axis.title = element_blank(),
               axis.ticks.y = element_blank(),
               legend.position = "bottom",
               axis.text.x = element_text(angle = 0,vjust = 0.4)) +
         geom_bar(stat = "identity", width = 0.7, position = position_dodge(width = 0.7)) +
         geom_text(aes(x = Var1, y = max(value), label = round(value, 2), fill = Var2), 
                   angle = 0, position = position_dodge(width = 0.7), size = 4.2)
    
    p + labs(fill = "")
    

    然后,我添加了以下代码。您可以在 stat_summary 中添加将 geom 更改为 point 的点。对于标签,我选择从ggplot_build() 获取数据并创建一个名为foo 的数据框。 (我认为还有其他方法可以完成相同的工作。)使用foo,我在最后添加了注释。

    p2 <- p +
          stat_summary(fun.y = mean, color = "red", geom = "line", aes(group = 1)) + 
          stat_summary(fun.y = mean, color = "black", geom ="point", aes(group = 1), size = 5,
                       show.legend = FALSE)
    
    # This is the data for your dots in the graph
    foo <- as.data.frame(ggplot_build(p2)$data[[4]])
    
    p2 +
    annotate("text", x = foo$x, y = foo$y + 0.5, color = "black", label = foo$y)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 2019-04-20
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      相关资源
      最近更新 更多