How to draw a Graph like attached one in R Studio

Hi All,

I have four data points with S.E (Std. Error received from R). I want to plug the point and their 99% & 95% confidence interval like attached. Could anyone help with this?

Male 0.4503 S.E 0.1558
Female 0.5841 S.E 0.1034
Age 21-34 0.6543 S.E. 0.2108
Age 45-54 0.4189 S.E 0.1850

Thank you!

Hi @Lernst. You have to calculate the 95% and 99% CI first and plot it with two geom_line and one geom_point. Finally, flip the coordinate.

library(tidyverse)

df <- data.frame(id = c("Male", "Female", "Age 21-34", "Age 45-54"),
           mean = c(.4503, .5841, .6543, .4189),
           se = c(.1558, .1034, .2108, .1850), stringsAsFactors = FALSE)

df %>%
  rowwise() %>%
  mutate(CI95 = list(c(mean + 1.96 * se, mean - 1.96 * se)),
         CI99 = list(c(mean + 2.58 * se, mean - 2.58 * se))) %>%
  unnest(c(CI95, CI99)) %>%
  ggplot() +
  labs(x = NULL, y = NULL) +
  geom_line(aes(x = id, y = CI99, group = id, color = id)) +
  geom_line(aes(x = id, y = CI95, group = id, color = id), size = 3) +
  geom_point(aes(x = id, y = mean, color = id), fill = "white", shape = 23, size = 3) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme_classic() +
  coord_flip()

Created on 2019-10-14 by the reprex package (v0.3.0)

5 Likes

Thanks, I will check the code. The graph is just what I need~ BTW, between each bar, there are grey lines, is there anyway to add this?

@Lernst. If you want the grey separate line background, the x values cannot be character. Change the x values to numeric and use scale_x_continuous to label the x axis. Then, in theme, use panel.grid.minor.y to plot the separate line.

library(tidyverse)

df <- data.frame(id = factor(c("Male", "Female", "Age 21-34", "Age 45-54")),
                 mean = c(.4503, .5841, .6543, .4189),
                 se = c(.1558, .1034, .2108, .1850), stringsAsFactors = FALSE)

df %>%
  rowwise() %>%
  mutate(CI95 = list(c(mean + 1.96 * se, mean - 1.96 * se)),
         CI99 = list(c(mean + 2.58 * se, mean - 2.58 * se))) %>%
  unnest(c(CI95, CI99)) %>%
  ggplot() +
  labs(x = NULL, y = NULL) +
  geom_line(aes(x = as.numeric(id), y = CI99, group = id, color = id)) +
  geom_line(aes(x = as.numeric(id), y = CI95, group = id, color = id), size = 3) +
  geom_point(aes(x = as.numeric(id), y = mean, color = id), fill = "white", shape = 23, size = 3) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  scale_x_continuous(labels = c("Age 21-34", "Age 45-54", "Female", "Male"), limits = c(0.7, 4.3), breaks = 1:4) +
  theme_classic() +
  theme(panel.grid.minor.y = element_line(color = "gray")) +
  coord_flip()

Created on 2019-10-14 by the reprex package (v0.3.0)

3 Likes

Thanks. When I am running the code in Rstudio, there is one error and I cannot see the graph: "Error: Column c(CI95, CI99) must be length 4 (the number of rows) or one, not 8"

What the problem?

@Lernst. Update the tidyr package and see if the problem still exists.

Thanks, it's R version's problem. After updating R version, then it works~

@raytong, you can add the grey lines with your original code by adding + geom_vline(xintercept=1:3+0.5, colour="grey70").

1 Like

@raytong Hello, I have one more question: is there anyway to control the order to display the graph? for example, I want to Age 21-34 is above Age 45-54 here. Thanks.

@Lernst. To arrange the order of the factor id by manually assign the levels. And change the order of labels argument in scale_x_continuous to correct label the x axis.

library(tidyverse)

df <- data.frame(id = factor(c("Male", "Female", "Age 21-34", "Age 45-54"), levels = c("Age 45-54", "Age 21-34", "Female", "Male")),
                 mean = c(.4503, .5841, .6543, .4189),
                 se = c(.1558, .1034, .2108, .1850), stringsAsFactors = FALSE)

df %>%
  rowwise() %>%
  mutate(CI95 = list(c(mean + 1.96 * se, mean - 1.96 * se)),
         CI99 = list(c(mean + 2.58 * se, mean - 2.58 * se))) %>%
  unnest(c(CI95, CI99)) %>%
  ggplot() +
  labs(x = NULL, y = NULL) +
  geom_line(aes(x = as.numeric(id), y = CI99, group = id, color = id)) +
  geom_line(aes(x = as.numeric(id), y = CI95, group = id, color = id), size = 3) +
  geom_point(aes(x = as.numeric(id), y = mean, color = id), fill = "white", shape = 23, size = 3) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  scale_x_continuous(labels = c("Age 45-54", "Age 21-34", "Female", "Male"), limits = c(0.7, 4.3), breaks = 1:4) +
  theme_classic() +
  theme(panel.grid.minor.y = element_line(color = "gray")) +
  coord_flip()

Created on 2019-10-15 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.