Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do have both count and percent on barcharts in ggplot2? R

Tags:

r

ggplot2

this is my data

data <- data.frame("Name" = c("Mark", "Jenny", "Linn"),
                   "Freq" = c("5","7", "3"),
                   "Percent" = c("33%", "47%", "20%"))

This is my plot

ggplot(data, aes(x=Name, y=Freq)) + 
  geom_bar(stat="identity", color = "black", fill="dodgerblue1")+
  geom_text(label=data$Freq, vjust=-1)

How can I have both my percent label next to my freq label preferably in parenthesis or separated by a comma?

like image 353
RL_Pug Avatar asked Nov 16 '25 03:11

RL_Pug


1 Answers

Compose the text label with paste/paste0.

ggplot(data, aes(x = Name, y = as.numeric(Freq))) + 
  geom_bar(stat = "identity", color = "black", fill = "dodgerblue1")+
  geom_text(label = with(data, paste(Freq, paste0('(', Percent, ')'))), vjust=-1) +
  ylim(0, 8)

enter image description here

like image 124
Rui Barradas Avatar answered Nov 17 '25 18:11

Rui Barradas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!