Advertisement
Guest User

Barplots vs Pie Charts

a guest
Jan 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. library(data.table)
  2. library(ggplot2)
  3.  
  4. dt <- data.table(party=c("'Dem ppl", "Repubs", "Wombats", "Kevin Bacon", "Trolls", "Good Space Guy", "Beer", "Ethics", "Goggles"),
  5. seats=c(57, 64, 5, 6, 7, 8, 56, 52, 57))
  6. dt$party <- factor(dt$party, levels=c("'Dem ppl", "Repubs", "Wombats", "Kevin Bacon", "Trolls", "Good Space Guy", "Beer", "Ethics", "Goggles"))
  7.  
  8. (TOTAL <- sum(dt$seats))
  9. print(sum(dt[party %in% c("Beer", "Ethics", "Goggles")]$seats)/TOTAL)
  10.  
  11. ggplot(dt, aes(x=party, seats, fill=party)) +
  12. geom_bar(stat = "identity") +
  13. scale_fill_brewer(palette="Set1") +
  14. theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
  15.  
  16. # With stacks to be fair
  17. ggplot(dt, aes(x=factor(c(1,1, 2,3,4,5, 6,6,6)), seats, fill=party)) +
  18. geom_bar(stat = "identity") +
  19. scale_fill_brewer(palette="Set1") +
  20. labs(title="but let's also consider stacks, to be fair") +
  21. theme(axis.text.x = element_blank(),
  22. axis.title.x = element_blank(),
  23. axis.ticks.x = element_blank())
  24.  
  25. # Pie charts can be good!
  26. (g <- ggplot(dt, aes(x="", seats, fill=party)) +
  27. geom_bar(stat = "identity", width=2, color="black") +
  28. coord_polar("y", direction=-1)) +
  29. scale_fill_brewer(palette="Set1") +
  30. labs(title="Ethical Beer Goggles ftw~") +
  31. geom_text(x=50, y=50, label="the real party", angle=-30) +
  32.  
  33. # Trying to add a thick vertical line cutting the plot in half
  34. geom_segment(aes(x = 0, y = 0, xend = 2.5, yend = 0), linetype="longdash", size=1.2) +
  35. geom_segment(aes(x = 0, y = 156, xend = 2.5, yend = 156), linetype="longdash", size=1.2) +
  36.  
  37. # Remove awkward polar coordinate crap and y-axis labels
  38. theme(axis.text = element_blank(),
  39. axis.ticks = element_blank(),
  40. panel.grid = element_blank(),
  41. axis.title.y = element_blank(),
  42. axis.title.x = element_blank())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement