Changing label geom_text in ggplot
2
0
Entering edit mode
26 days ago
Bine ▴ 90

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 • 483 views
ADD COMMENT
2
Entering edit mode

Can't you instead of filtering on "Responder" just use a facet_wrap to show responders and non-responders separately? If your groups are exactly evenly sized, you can also calculate the non-responder rate as group size - n responders in that group.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
2
Entering edit mode
26 days ago
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

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
2
Entering edit mode

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)
ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode
26 days ago
Jeremy ▴ 930

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

ADD COMMENT

Login before adding your answer.

Traffic: 1810 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6