Skip to content

Instantly share code, notes, and snippets.

@sebastiansauer
Created July 12, 2016 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastiansauer/b665efe45a3cd91e71bc9f4ac70b19f1 to your computer and use it in GitHub Desktop.
Save sebastiansauer/b665efe45a3cd91e71bc9f4ac70b19f1 to your computer and use it in GitHub Desktop.
tabulate multiple columns and plot the frequencies
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