Last active
August 16, 2019 21:33
-
-
Save Paul-Yuchao-Dong/f6d941d20e8f3e258eadb2c12a19d35d to your computer and use it in GitHub Desktop.
Practice reordering of stacked bar chart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "sorted stacked bars" | |
output: github_document | |
--- | |
# Practice reordering of stacked bar chart | |
inspiration: https://github.com/larissakostiw/-TidyTuesday/blob/master/roman.md | |
```{r message=FALSE, warning=F} | |
library(tidyverse) | |
library(ggthemes) | |
library(RColorBrewer) | |
library(ggthemes) | |
emperors <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-08-13/emperors.csv") | |
``` | |
```{r} | |
stacked <- emperors %>% | |
mutate(killer=as.factor(killer)) %>% | |
mutate(cause=as.factor(cause)) %>% | |
group_by(cause) %>% | |
mutate(count=n())%>% | |
ungroup() %>% | |
group_by(killer) %>% | |
mutate(count2=n())%>% | |
ungroup() %>% | |
mutate(cause=fct_reorder(cause, count), | |
killer = fct_reorder(killer, count2) | |
) %>% | |
group_by(cause, killer) %>% | |
summarise(total = n()) %>% | |
arrange(cause)%>% | |
ggplot(aes(cause, total)) + | |
geom_col(aes(fill = factor(killer)), colour="white") + | |
# geom_text(aes(x=killer, y=pos, label=killer), size=2) + | |
#scale_fill_brewer()+ | |
scale_fill_manual(values = colorRampPalette(economist_pal()(8))(14))+ | |
theme_clean() + | |
labs(title="Roman Emperors", | |
subtitle = "How they died and what/whom killed them", | |
x= "Cause", | |
y="Total Deaths", | |
caption= "Source: Wikipedia Zonination. Plot: @LarissaKostiw", | |
fill = "Killer")+ #Error: DOesn't know how to add stacked to the plot. | |
coord_flip()+ | |
theme( | |
panel.background = element_rect(fill = "black", color = "white", linetype = "dashed"), | |
#panel.background = element_blank(), | |
plot.background = element_rect(fill = "black"), | |
plot.caption = element_text(color = "white"), | |
plot.title = element_text(size = 15, face = "bold", color = "white", hjust = 0.5), | |
plot.subtitle = element_text(size = 12, face = "bold", color = "white", hjust = 0.5), | |
axis.text = element_text(color = "white", size = 12), | |
axis.title = element_text(face="bold", color = "white", size = 12), | |
legend.text = element_text(size = 10, color = "white"), | |
legend.title = element_text(color = "white"), | |
legend.background = element_rect(fill = "black", color = "white", linetype = "dashed")) | |
stacked | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment