adding percentage on top of columns while maintaining counts in Y axis

here is my dataframe

tata3 <- data.frame( stringsAsFactors = TRUE, Subtype = c("OPC", "Hypopharynx", "Larynx"),  
alive = c(88, 22, 100), dead = c(12, 55, 17), uncertain = c(10, 2, 2), total = c(186,46,202))

I enter the following code to generate a nice graph.


ggplot (data = tata3, aes(x=Subtype, y =total, fill=Subtype)) + 
geom_col() + scale_fill_manual(values = c("Red", "Green", "Blue"))

what I want is this..

  1. The Y axis maintained as it is (showing the total values as they are)
  2. The value in % displayed on top of the graph (in this case OPC will be "42.8%")...with the option to also display the actual value (in the case of OPC "186") besides the percentage.

I've tried various permutations of geom_text including stat_count...but I think I'm failing to specify exactly what Rstudio needs to use to calculate the percentage from. E.g. the code below gives a massive error.

ggplot (data = tata3, aes(x=Subtype, y =total, fill=Subtype)) + geom_col() + scale_fill_manual(values = c("Red", "Green", "Blue")) + geom_text (aes(label=after_stat('prop*100'), group=1),stat='total',nudge_y=0.125,va='bottom', format_string='{:.1f}%')

Can someone please help. Many thanks

That's pretty straightforward.

Here's the code:

library(tidyverse)
library(scales)

tata3 %>% 
  mutate(perc=total/sum(total)) %>% 
    ggplot() + 
  geom_col(aes(Subtype,total,fill = Subtype)) + 
  geom_text(aes(Subtype,total + 5,label = percent(perc)))

sorry, I get following error message when I enter your code:

Error in percent(perc) : could not find function "percent"

not sure what's happening here

Did you load library(scales)? percent is a function called from scales

yes, sorry, just realised the error!.

I've tried extrapolating from your code and entered this, to allow me to get % as well as the actual value

tata3 %>% mutate(perc=total/sum(total)) %>% ggplot() + geom_col(aes(Subtype,total,fill = Subtype)) + 
geom_text(aes(Subtype,total + 5,label = percent(perc))) + 
geom_text(aes(Subtype, total + 1, label= total)) 

whats the actual code to try and get the position of the texts adjusted so that they don't overlap?
Many thanks

Think I figured it out by entering this code

tata3 %>% mutate(perc=total/sum(total)) %>% ggplot() + geom_col(aes(Subtype,total,fill = Subtype)) + geom_text(aes(Subtype,total + 5,label = percent(perc))) + 
geom_text(aes(Subtype, total - 3, label= total))

Any way in which you can get the % to appear in brackets e.g. (46.5%) ? many thanks

If you want in brackets, use this code.

tata3 %>% 
mutate(perc=total/sum(total)) %>% 
ggplot() + geom_col(aes(Subtype,total,fill = Subtype)) + 
geom_text(aes(Subtype,total + 5,label = glue::glue("{total}({percent(perc)})")))

Many Thanks,

Worked exactly the way I wanted it to

Glad to know it helped. R is very powerful. Please keep learning.
Cheers
Sanjay

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.