0

I tried to add the mean to a boxplot in ggplot, but the mean is added next to the boxplot and not within the box. Unfortunately, I can't find any solutions on the internet. Can someone help me? My data looks like this:

dput(test2)
structure(list(station = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
    date = c("01.01.2018", "02.01.2018", "03.01.2018", "04.01.2018", 
    "05.01.2018", "01.01.2018", "02.01.2018", "03.01.2018", "04.01.2018", 
    "05.01.2018", "01.01.2018", "02.01.2018", "03.01.2018", "04.01.2018", 
    "05.01.2018", "01.01.2018", "02.01.2018", "03.01.2018", "04.01.2018", 
    "05.01.2018", "01.01.2018", "02.01.2018", "03.01.2018", "04.01.2018", 
    "05.01.2018", "01.01.2018", "02.01.2018", "03.01.2018", "04.01.2018", 
    "05.01.2018", "01.01.2018", "02.01.2018", "03.01.2018", "04.01.2018", 
    "05.01.2018", "01.01.2018", "02.01.2018", "03.01.2018", "04.01.2018", 
    "05.01.2018"), number = c(0, 0, 1.25, 0, 0, 0.761636107, 
    0.917030568, 0.526315789, 0.462555066, 0.728476821, 0.625, 
    3.75, 1.276595745, 0, 0, 1.636363636, 1.25748503, 0.176470588, 
    1.220930233, 1.031518625, 1, 0, 4, 1, 0, 0.174978128, 0.131004367, 
    0.197368421, 0.396475771, 0.198675497, 1.25, 1.25, 0, 0, 
    0, 0, 0.718562874, 0, 0.523255814, 0), time = c(1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    3L, 3L, 4L, 4L, 4L, 4L, 4L)), class = "data.frame", row.names = c(NA, 
-40L))

This is my code:

options(stringsAsFactors = FALSE) 
input5 <- "C:\\Users\\test2.csv" 

test2<- read.csv(input5, sep=";") 

library(lubridate) 
library(dplyr) 
library(ggplot2) 

means <- aggregate(number ~ station, test2, mean) 
p2 <- test2 %>% 
  mutate(
    date = dmy(date),
    station = as.factor(station), 
    time = as.factor(time)
  ) %>% 
  group_by(time) %>% 
  ggplot(aes(x = station, y = number, fill = time)) + 
  geom_boxplot() + 
  theme_bw() + 
  stat_summary(fun.y = mean, colour = "red", geom = "point", shape = 16, size = 1, show_guide = FALSE) + 
  theme(legend.position = "none") 
p2
wibeasley
  • 5,000
  • 3
  • 34
  • 62
Dolphin94
  • 319
  • 1
  • 2
  • 8
  • Please include the code in your question. – markus Feb 24 '19 at 16:10
  • 1
    You need to use the same "dodge width" in both boxes and points. `p <- position_dodge(width = 0.9)`; `ggplot(mtcars, aes(x = factor(am), y = mpg, fill = factor(vs))) + geom_boxplot(position = p) + stat_summary(fun.y=mean, colour="red", geom="point", shape=16, size=1, show.legend = FALSE, position = p)` – Henrik Feb 24 '19 at 16:18
  • See [What is the width argument in position_dodge?](https://stackoverflow.com/questions/34889766/what-is-the-width-argument-in-position-dodge) for a more thorough explanation. The most relevant point here: "the width in data units of points (`geom_point`) [...] is zero. Thus, if you need to dodge such elements, you need to specify a relevant virtual width, on which dodge calculations then are based." – Henrik Feb 24 '19 at 16:25
  • Thank you very much Henrik! This is working. – Dolphin94 Feb 24 '19 at 16:27

0 Answers0