Oiramsch
Oiramsch

Reputation: 3

Calculate percentage value of grouped value in geom_bar while showing count per grouped value

I'm trying to show data of count & percentage in a stacked bar with ggplot2. Data ist here

https://github.com/oiramsch/CvsRWO/blob/master/colorList.csv

I tried this:

p <- colorList %>% ggplot(mapping = aes(x=factor(age, levels = c("young","old")),fill=factor(answer_correct, levels = c(0,1), labels = c("False","Correct"))))

and then

p + geom_bar(stat="count", position="stack")+facet_grid(~condition)+labs(title="Colors",subtitle = "per setsize grouped by age", x="", y="Count answer correct", fill="Answer correct")+geom_text(aes(label = paste(..count..,"\n",scales::percent(..count../1298,0.01))),position=position_stack(0.5),stat="count")

as a result I receive:

Bar Chart

The problem is that I use a fixed value of 1298 as the total value to calculate the percentage share ( which is unfortunately not true for all columns). So I'm looking for an internal variable of ggplot which I could use to access the sum per column or even better the percentage of the false/correct values per set size. But I was not able to figure out how to do this. Of cause I could summarise the data in advance but I thought there must be a solution inside of ggplot...If there is someone, who has an idea how I can fix my code I would highly appreciate it.

Upvotes: 0

Views: 2066

Answers (2)

Duck
Duck

Reputation: 39613

I would suggest this approach summarising values which can be an option for you:

library(ggplot2)
library(dplyr)
#Data
colorList <- read.csv('https://raw.githubusercontent.com/oiramsch/CvsRWO/master/colorList.csv',stringsAsFactors = F)
#Aggregate
colorList %>% mutate(age=factor(age, levels = c("young","old")),
             answer_correct=factor(answer_correct, levels = c(0,1))) %>%
  group_by(age,condition,answer_correct) %>% summarise(N=n()) %>% ungroup() %>%
  group_by(age,condition) %>% 
  mutate(Total=sum(N),Percent=N/Total,
         Lab=paste0(N,' (',paste0(round(100*Percent,0),'%'),')')) -> Sums
#Plot
ggplot(Sums,aes(x=age,y=N,fill=answer_correct))+
  geom_bar(stat='identity',position = position_stack())+
  facet_wrap(.~condition,scales = 'free')+
  geom_text(aes(label=Lab),position = position_stack(vjust = .5),size=3)+
  geom_text(aes(y=Total,label=Total),vjust=-0.25,size=3)

Output:

enter image description here

Upvotes: 2

Liman
Liman

Reputation: 1300

Are you looking for (..count..)/sum(..count..)?

p + 
  geom_bar(stat="count", position="stack")+
  facet_grid(~condition)+
  labs(title="Colors",subtitle = "per setsize grouped by age", x="", y="Count answer correct", fill="Answer correct")+
  geom_text(aes(label = paste(..count..,"\n",
                              scales::percent(..count../sum(..count..), 0.01))),
            position=position_stack(0.5),stat="count")

Upvotes: 1

Related Questions