jessicacardoso

group_columns

Nov 19th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.83 KB | None | 0 0
  1. library(dplyr)
  2. library(magrittr)
  3.  
  4. counts <- df %>%
  5.   group_by(source, round) %>%
  6.   summarize(Freq = n())
  7.  
  8. counts2 <- counts %>%
  9.             group_by(source) %>%
  10.             summarise(Freq = sum(Freq)) %>%
  11.             arrange(desc(Freq)) %>% head(10)
  12.  
  13. counts <- counts %>% filter( source %in% counts2$source)
  14.  
  15. #"#ff3333", "#00cc66"
  16. ########################
  17. colors <- c('#d9b3ff', '#000000', '#7300e6', '#a64dff', '#8000ff', '#330066',
  18.             '#99ccff', '#0039e6', '#3366ff', '#809fff', '#99b3ff', '#e6ecff')
  19.  
  20. ggplot(counts, aes(x = candidate, y = Freq, fill = source)) +
  21.   geom_bar(position = position_stack(), stat = "identity", width = .7) +
  22.   labs(title = "Frequency by candidate\n", x = "", y = "Frequency\n") +
  23.   theme_light()+
  24.   theme(text = element_text(size=20)) +
  25.   coord_flip() +
  26.   scale_fill_manual(values=colors)
Add Comment
Please, Sign In to add comment