Adding asterisk to flag significance on ggplot

Viewed 48

I have this bar graph, and want to show asterisks that flag significance (**).

viz_data_one <- tibble(
  age_group = c(rep("Young Adult", 4), rep("Older Adult", 4)),
  MemoryAccuracy = c(32.8, 28.448, 27.672, 27.075, 29.667, 28.944, 27.556, 28.889),
  upper = MemoryAccuracy + 1.76,
  lower = MemoryAccuracy - 1.76,
  reward = rep(c("Self High Value", "Self Low Value", "Other High Value", "Other Low Value"), 2)
) %>% 
  mutate(
    reward = as_factor(reward) %>% fct_relevel("Self High Value",
                                               "Self Low Value",
                                               "Other High Value",
                                               "Other Low Value"))
viz_data_one <- viz_data_one %>%
  mutate(upper = MemoryAccuracy + ifelse(age_group == "Young Adult", 1.76, 1.94),
         lower = MemoryAccuracy - ifelse(age_group == "Young Adult", 1.76, 1.94))
      
p <- viz_data_one %>% 
  ggplot(aes(x = age_group,
             y = MemoryAccuracy,
             fill = reward,
             ymin = lower,
             ymax = upper)) +
  geom_col(width = .5, position = position_dodge(.6),
           color = "black", key_glyph = "polygon") +
  geom_errorbar(width = .1, position = position_dodge(.6)) +
  scale_fill_manual(values = c("#E495A5", "#ABB065", "#39BEB1", "#ACA4E2" )) +
  
  labs(
    x = "Age Group",
    y = "Memory Accuracy (%)",
    fill = NULL,
    title = ""
  ) +
  theme(
    plot.margin = unit(c(1, 1, 1, 1), "cm"),
    panel.background = element_blank(),
    plot.title = element_text(size = 14, face = "bold",
                              hjust = 0.5,
                              margin = margin(b = 15)),
    axis.line = element_line(color = "black"),
    axis.title = element_text(size = 14, color = "black",
                              face = "bold"),
    axis.text = element_text(size = 18, color = "black"),
    axis.text.x = element_text(margin = margin(t = 10)),
    axis.text.y = element_text(size = 14),
    axis.title.y = element_text(margin = margin(r = 10)),
    axis.ticks.x = element_blank(),
    legend.position = c(0.90, 0.99),
    legend.background = element_rect(color = "black"),
    legend.text = element_text(size = 15),
    legend.margin = margin(t = 5, l = 5, r = 5, b = 5),
    legend.key = element_rect(color = NA, fill = NA)
  ) +
  guides(
    fill = guide_legend(
      keywidth = .5,
      keyheight = .5,
      default.unit= "cm"
    )
  )


p + expand_limits(y = 40)

I want to add asterisk stars to the younger adult group (between self high value and self low value) and have everything else as ns. I've tried a few different things with ggpubr and geom_signif but had no luck.

1 Answers

I ended up solving this by drawing my own using geom_line

p + 
  geom_line(data=tibble(x=c(1.77,1.929), y=c(40,40)),
            aes(x=x, y=y), inherit.aes = FALSE) +
  geom_text(data=tibble(x=1.849, y = 41),
            aes(x=x, y=y, label = "**"),
            inherit.aes = FALSE)+
  geom_line(data=tibble(x=c(1.929,2.22), y=c(33,33)),
            aes(x=x, y=y), inherit.aes = FALSE) +
  geom_text(data=tibble(x=2.0745, y = 34.5),
            aes(x=x, y=y, label = "ns"),
            inherit.aes = FALSE)+
  geom_line(data=tibble(x=c(0.77,1.22), y=c(40,40)),
            aes(x=x, y=y), inherit.aes = FALSE) +
  geom_text(data=tibble(x=1, y = 41.5),
            aes(x=x, y=y, label = "ns"),
            inherit.aes = FALSE)
Related