Miha
Miha

Reputation: 2884

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:

enter image description here

But beside the red line which is marking total average by question (i.e. "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

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.

enter image description here

I've also looked at:

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

Upvotes: 2

Views: 15261

Answers (1)

jazzurro
jazzurro

Reputation: 23574

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. For labels, I chose to get data from ggplot_build() and crated a data frame called foo. (I think there are other ways to do the same job.) Using foo, I added annotation in the end.

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)

enter image description here

Upvotes: 8

Related Questions