I have a dataframe in R that I want to plot in a faceted ggplot bar chart.
I use this code in ggplot:
ggplot(data_long, aes(x = partei, y = wert, fil
No need of additionale packages, you can achieve this with plain ggplot:
The complete solution:
data_long %>%
mutate(kat_partei = paste0(kat, '_', partei),
kat_partei = forcats::fct_reorder(kat_partei, wert)) %>%
ggplot(aes(x = kat_partei, y = wert, fill = kat, width=0.75)) +
geom_bar(stat = "identity", show.legend = FALSE) +
scale_x_discrete(name=NULL, labels=function(x) sub('^.*_(.*)$', '\\1', x)) +
scale_fill_brewer(palette="Set2") +
coord_flip() +
facet_wrap(~kat, scales='free_y') +
labs(y = "Wähleranteil [ % ]") +
theme_bw() + theme(strip.background = element_blank(),
panel.grid.major = element_line(colour = "grey80"),
panel.border = element_blank(),
axis.ticks = element_line(size = 0),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank())
further hints:
geom_col()
instead of geom_bar(stat = "identity")
show.legend
argument instead of guides(fill=FALSE)