Created
July 12, 2016 10:12
-
-
Save sebastiansauer/b665efe45a3cd91e71bc9f4ac70b19f1 to your computer and use it in GitHub Desktop.
tabulate multiple columns and plot the frequencies
This file contains 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
library(dplyr) | |
data(Wage, package = "ISLR") | |
Wage %>% | |
mutate(wage_f = ntile(wage, 2)) %>% # bin it | |
group_by(wage_f, health, race) %>% | |
summarise(count = n()) %>% | |
ggplot(aes(x = factor(wage_f), y = count, fill = race)) + | |
geom_bar(stat = "identity") + | |
facet_wrap(~health) | |
# http://rpubs.com/sebastian_sauer/195378 | |
Wage %>% | |
mutate(wage_f = ntile(wage, 2)) %>% # bin it | |
group_by(wage_f, health, race) %>% | |
summarise(count = n()) %>% | |
mutate(prop = count/sum(count)) %>% | |
select(-count) %>% | |
ggplot(aes(x = factor(wage_f), y = prop, fill = race)) + | |
geom_bar(stat = "identity") + | |
facet_wrap(~health) | |
# http://rpubs.com/sebastian_sauer/195379 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment