This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Changing label geom_text in ggplot

Good afternoon,

Can anyone give me a hint on this:

I would like to add in this plot not only the numbers of the Responder (on which the ORR % and n= X is based on), instead I would also like to add the number of Non-Responder. The issue is that since I filter on Responder in line 5, I do not longer have the Non-Responder data available. But I need to filter on Responder in line 5 because for the ORR in % I want it only for the Responder. Anyonw has one idea?

p<-metadata %>%
  dplyr::count(Sex,side, ORR) %>% 
  dplyr::group_by(Sex) %>%
  dplyr::mutate(prop = 100 * n / sum(n)) %>%
  dplyr::filter( ORR == "Responder" )%>%
  ggplot(aes(x =Sex, y = prop, fill =side)) +
  geom_col(position = position_dodge()) +
  geom_text(aes(label =paste0(round(prop),"%")),
  position = position_dodge(.9), vjust = 1.2) +
  geom_text(aes(label =paste0("n = ",n)),
  position = position_dodge(.9), vjust = -0.5
  )

enter image description here

ggplot

Thank you, I tried this. but then I get two plots. I just want to add the n of the Non-Responder in the plot.

2 answers

library(dplyr)
library(ggplot2)


metadata <- data.frame(Sex = sample(c("M", "F"), 100, replace = TRUE), 
                       side = sample(c("left", "right"), 100, replace = TRUE), 
                       ORR = sample(c("Responder", "Non.Responder"), 100, replace = TRUE))


p <- metadata %>%
  dplyr::count(Sex,side, ORR) %>% 
  dplyr::group_by(Sex) %>%
  dplyr::mutate(prop = 100 * n / sum(n)) %>%
  ggplot(aes(x =Sex, y = prop, fill =side)) +
  geom_col(position = position_dodge()) +
  geom_text(aes(label =paste0(round(prop),"%")),
            position = position_dodge(.9), vjust = 1.2) +
  geom_text(aes(label =paste0("n = ",n)),
            position = position_dodge(.9), vjust = -0.5) +
  facet_wrap(~ORR)

enter image description here

Thank you very much. I dont really need the plots of the non-responder, I just want to put the number of non-responder into the n=. As shown in the plot below:

enter image description here

I'm one of these luddites that doesn't like using tidyverse very much so I just did it with tapply, maybe someone else can clean it up. Main pitfall of this code is that it's vulnerable to the ordering of the factor

library(dplyr)
library(ggplot2)


metadata <- data.frame(Sex = sample(c("M", "F"), 100, replace = TRUE), 
                       side = sample(c("left", "right"), 100, replace = TRUE), 
                       ORR = sample(c("Responder", "Non.Responder"), 100, replace = TRUE))


p <- metadata %>%
  dplyr::count(Sex,side, ORR) %>% 
  dplyr::group_by(Sex) %>%
  dplyr::mutate(prop = 100 * n / sum(n))

n.lab <- tapply(p$n, paste(p$Sex, p$side), \(x) paste(c(x[2], sum(x)), collapse = " / "))

p <- p %>%
dplyr::filter( ORR == "Responder")

p$n.lab <- n.lab

ggplot(p, aes(x =Sex, y = prop, fill =side)) +
  geom_col(position = position_dodge()) +
  geom_text(aes(label =paste0(round(prop),"%")),
            position = position_dodge(.9), vjust = 1.2) +
  geom_text(aes(label =paste0("n = ",n.lab)),
            position = position_dodge(.9), vjust = -0.5)

Thank you very much, thats exactly what I was looking for :). The code is maybe a bit complex, but it does the job.

This doesn't use ggplot2, but you could add "n=" using GIMP or Adobe Illustrator.

Log in to answer this question.