Reputation: 1875
I have the created the following bar plot with ggplot:
library(tidyverse)
temp<-tribble(
~kt, ~yes, ~'no', ~'NA',
"Berne", 47,33, 0,
"Basel", 60,45,0,
"Geneva", 64,61,0,
"Zurich", 19,107,3
)
temp2 <- gather(temp, ' ', val, -kt)
ggplot(temp2, aes(fct_rev(kt), val, fill = ` `)) +
geom_col(position = 'fill', color='black') +
geom_text(aes(label = val), position = position_fill(vjust = 0.5), size=3) +
scale_y_continuous(labels = scales::percent_format())+theme_bw()+
labs(x='Canton', y='Percentage')+coord_flip()+ scale_fill_grey(start = 0.9, end = .5)+
guides(fill=guide_legend(title="Label", reverse=T))
However, those 0-values on the right side look very disturbing. Is there a way to suppress them in ggplot?
Upvotes: 1
Views: 636
Reputation: 7433
You can filter them with:
temp2 <- gather(temp, ' ', val, -kt)
filter(temp2, val>0) %>%
ggplot(aes(fct_rev(kt), val, fill = ` `)) +
geom_col(position = 'fill', color='black') +
geom_text(aes(label = val), position = position_fill(vjust = 0.5), size=3) +
scale_y_continuous(labels = scales::percent_format())+theme_bw()+
labs(x='Canton', y='Percentage')+coord_flip()+ scale_fill_grey(start = 0.9, end = .5)+
guides(fill=guide_legend(title="Label", reverse=T))
Is that what you were looking for?
Upvotes: 3