简体   繁体   English

ggplot2:添加显示均值的线和点(stat_summary)

[英]ggplot2: add line and points showing means (stat_summary)

So I am using this data frame: 所以我正在使用此数据框:

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))

And with this 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="")

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

I produce output: 我产生输出:

在此处输入图片说明

But beside the red line which is marking total average by question (ie "dolgoročno", "krizno" etc.) I would like to add points and next to the bars as well as labels of the individual question group mean 但是在红线旁边是按问题标出的总平均值(即“dolgoročno”,“ krizno”等)。我想在条形图以及各个问题组的标签旁边添加点

My output should look something like the picture below, (I did it in paint), where the black dots represent my desired points and the value 3.6 of the first dot is the average of (6,2,4,2.5) and represents my desired value labels. 我的输出应类似于下图(我在油漆中完成),其中黑点代表我想要的点,第一个点的值3.6是(6,2,4,2.5)的平均值,代表我的期望值标签。

在此处输入图片说明

I've also looked at: 我还看了:

Plot average line in a facet_wrap 在facet_wrap中绘制平均线

ggplot2: line connecting the means of grouped data ggplot2:连接分组数据的方式的线

How to label graph with the mean of the values using ggplot2 如何使用ggplot2用值的平均值标记图

One option would be the following. 一种选择是以下。 I followed your code and added a few lines. 我按照您的代码并添加了几行。

# 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 = "")

Then, I added the following code. 然后,我添加了以下代码。 You can add dots changing geom to point in stat_summary. 您可以添加更改geom的pointpoint stat_summary。 For labels, I chose to get data from ggplot_build() and crated a data frame called foo . 对于标签,我选择从ggplot_build()获取数据,并创建一个名为foo的数据框。 (I think there are other ways to do the same job.) Using foo , I added annotation in the end. (我认为还有其他方法可以完成相同的工作。)使用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)

在此处输入图片说明

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

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