0

enter image description here

To create this plot I used the following code:

test_data %>%
  mutate(dot_color = ifelse(row_number() == 13, "red", "black")) %>%
  ggplot(aes(x = month, y = mean_score)) +
  geom_line(aes(group = 1), size = 1, color = "gray40") +
  geom_point(aes(color = dot_color), size = 5) +
  geom_errorbar(aes(ymin = mean_score - sd(mean_score), ymax = mean_score + sd(mean_score)), width = 0.2) +
  scale_color_identity() +
  scale_y_continuous(limits = c(-0.08, 0.001), expand = c(0, 0)) +
  xlab("Month") +
  ylab("Mean Score") +
  ggtitle("Mean Score Over Time") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        plot.title = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major.y = element_line(color = "gray80"))

How can I change the display of the standard deviation? I want it to appear as a semi-transparent continuous layer and not just as a line over each point.

Thanks for your help in advance.

Best,

Marsupilami

dput(test_data) gives me the following outprint in the console:

structure(list(month = c("2010_09", "2010_10", "2010_11", "2010_12", 
"2011_01", "2011_02", "2011_03", "2011_04", "2011_05", "2011_06", 
"2011_07", "2011_08", "2011_09", "2011_10", "2011_11", "2011_12", 
"2012_01", "2012_02", "2012_03", "2012_04", "2012_05", "2012_06", 
"2012_07", "2012_08", "2012_09"), mean_score = c(-0.0410314783067388, 
-0.058019985508201, -0.0356711755233494, -0.0372229691095461, 
-0.0449221657669101, -0.0361966978350602, -0.0528777079508544, 
-0.0421520168683535, -0.0371713600130145, -0.0420191475470994, 
-0.0518498479566862, -0.049552355942377, -0.06296103117506, -0.0564000134084205, 
-0.0417076530269435, -0.0583660412336386, -0.0552066721949353, 
-0.0504569837526206, -0.0327654798534799, -0.0407763154279434, 
-0.0474108095497317, -0.0474328848540265, -0.0450817442038999, 
-0.0473148373202061, -0.0497317904374364)), row.names = c(NA, 
-25L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000198c01bbb40>)
  • Can you make your question reproducible and provide `dput(test_data)`? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jrcalabrese Apr 23 '23 at 13:08

1 Answers1

1

You can use geom_ribbon() with almost the same arguments as you did with geom_errorbar().

test_data %>%
  mutate(dot_color = ifelse(row_number() == 13, "red", "black")) %>%
  ggplot(aes(x = month, y = mean_score)) +
  geom_line(aes(group = 1), size = 1, color = "gray40") +
  geom_point(aes(color = dot_color), size = 5) +
  geom_ribbon(aes(ymin = mean_score - sd(mean_score), 
                  ymax = mean_score + sd(mean_score), 
                  group = 1), alpha = 0.3) +
  scale_color_identity() +
  scale_y_continuous(limits = c(-0.08, 0.001), expand = c(0, 0)) +
  xlab("Month") +
  ylab("Mean Score") +
  ggtitle("Mean Score Over Time") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        plot.title = element_text(hjust = 0.5, vjust = 0.5),
        panel.grid.major.y = element_line(color = "gray80"))

enter image description here

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30