Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Correctly plotting geom_errorbar() to fit the min/max of a group

I have some data which takes in predictions and actual results. The predictions take on a range and I plot the average also.

I want to add a geom_errorbar() to display the range by each group. The best I can do is to take the range across the whole group.

I currently have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x %>%
  ggplot(aes(x = Fecha, y = MC_Clientes, color = Type, group = Zona)) +
  geom_point() +
  stat_summary(geom = "point", fun = "mean", col = "black", size = 3, shape = 24, fill = "red") +
  geom_errorbar(aes(ymin = min(MC_Clientes), ymax = max(MC_Clientes)), width = 0.2) +
  #geom_hline(aes(yintercept = mean(MC_Clientes)), color="blue") +
  facet_wrap(~ Zona)

enter image description here

But I would like only the error bars to display for the predictions – and to only consider the min/max for that given day.

Data:

    x = structure(list(Fecha = structure(c(18993, 19448, 19723, 19631, 
19997, 19083, 19631, 19174, 19631, 19539, 19905, 18993, 19083, 
19266, 19448, 19083, 19358, 19723, 19539, 19358, 19358, 19814, 
19266, 17987, 19723, 19723, 19083, 19723, 17987, 18993, 18993, 
19174, 19814, 19905, 19997, 19539, 19266, 19358, 19905, 18993, 
19723, 18993, 19174, 19814, 19448, 19358, 18262, 19266, 19083, 
18536, 18628, 19448, 19083, 19997, 19997, 19723, 19448, 19723, 
19997, 19266), class = "Date"), MC_Clientes = c(`10` = 3.30914630982938, 
`163` = 3.40705102004165, `148` = 3.27210950513308, `225` = 3.30737187089665, 
`114` = 3.27216488807394, `156` = 3.29952915488204, `110` = 3.39960342129031, 
`65` = 3.44020887082005, `226` = 3.3477486021078, `72` = 3.37828428718909, 
`89` = 3.35889902349877, `130` = 3.24841711499344, `33` = 3.39557631661649, 
`96` = 3.30867139996011, `48` = 3.29220186390159, `39` = 3.38775370812839, 
`11` = 3.33638404617856, `28` = 3.34128523097663, `196` = 3.32642364225741, 
`20` = 3.30914630982938, `15` = 3.32250928717776, `59` = 3.38775370812839, 
`215` = 3.30737187089665, 3.23830671159728, `143` = 3.262723490499, 
`144` = 3.28580416922959, `154` = 3.19317306859439, `146` = 3.22387486690392, 
3.50997312704934, `125` = 3.2916234598844, `128` = 3.27210950513308, 
`68` = 3.42813783609873, `57` = 3.36530584016227, `204` = 3.43236203716387, 
`118` = 3.3861339757903, `192` = 3.4510934126958, `211` = 3.26617681913074, 
`140` = 3.24841711499344, `87` = 3.43574132492634, `2` = 3.30010088087279, 
`27` = 3.33539993355129, `129` = 3.27777616345452, `184` = 3.43236203716387, 
`55` = 3.31622658677549, `42` = 3.34351801788172, `132` = 3.28169122393979, 
3.18803129400015, `92` = 3.35020429586366, `157` = 3.39074839564108, 
3.11563497015824, 3.40541198110811, `166` = 3.29952915488204, 
`38` = 3.29220186390159, `111` = 3.25522990668431, `113` = 3.26178511373003, 
`150` = 3.24841711499344, `167` = 3.39074839564108, `22` = 3.30010088087279, 
`233` = 3.40771653641682, `214` = 3.26936263234189), Zona = c("A", 
"B", "B", "B", "A", "B", "A", "A", "B", "A", "A", "B", "A", "A", 
"A", "A", "A", "A", "B", "A", "A", "A", "B", "B", "B", "B", "B", 
"B", "A", "B", "B", "A", "A", "B", "A", "B", "B", "B", "A", "A", 
"A", "B", "B", "A", "A", "B", "B", "A", "B", "B", "A", "B", "A", 
"A", "A", "B", "B", "A", "B", "B"), Type = c("Predictions", "Predictions", 
"Predictions", "Predictions", "Predictions", "Predictions", "Predictions", 
"Predictions", "Predictions", "Predictions", "Predictions", "Predictions", 
"Predictions", "Predictions", "Predictions", "Predictions", "Predictions", 
"Predictions", "Predictions", "Predictions", "Predictions", "Predictions", 
"Predictions", "Actual", "Predictions", "Predictions", "Predictions", 
"Predictions", "Actual", "Predictions", "Predictions", "Predictions", 
"Predictions", "Predictions", "Predictions", "Predictions", "Predictions", 
"Predictions", "Predictions", "Predictions", "Predictions", "Predictions", 
"Predictions", "Predictions", "Predictions", "Predictions", "Actual", 
"Predictions", "Predictions", "Actual", "Actual", "Predictions", 
"Predictions", "Predictions", "Predictions", "Predictions", "Predictions", 
"Predictions", "Predictions", "Predictions")), row.names = c(NA, 
-60L), class = c("tbl_df", "tbl", "data.frame"))

>Solution :

Are you looking for something more like this?

library(tidyverse)

x |>
  group_by(Type, Zona) |>
  mutate(min = min(MC_Clientes),
         max = max(MC_Clientes))|>
  ggplot(aes(x = Fecha, y = MC_Clientes, color = Type, group = Zona)) +
  geom_point()+
  stat_summary(geom = "point", fun = "mean", col = "black", size = 3, shape = 24, fill = "red") +
  geom_errorbar(aes(ymin = min, ymax = max), width = 0.2) +
  #geom_hline(aes(yintercept = mean(MC_Clientes)), color="blue") +
  facet_wrap(~ Zona)

Edit based on comment:


x |>
  group_by(Fecha,Type, Zona) |>
  mutate(min = min(MC_Clientes),
         max = max(MC_Clientes))|>
  ggplot(aes(x = Fecha, y = MC_Clientes, color = Type, group = Zona)) +
  geom_point()+
  stat_summary(geom = "point", fun = "mean", col = "black", size = 3, shape = 24, fill = "red") +
  geom_errorbar(aes(ymin = min, ymax = max), width = 0.2) +
  #geom_hline(aes(yintercept = mean(MC_Clientes)), color="blue") +
  facet_wrap(~ Zona)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading