celestialgod

barcharts

May 24th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.41 KB | None | 0 0
  1. library(lattice)
  2. library(dplyr)
  3. dat <- data.frame(fruit = rep(c("apple", "orange", "banana"), each=2),
  4.                   count = c(20, 40, 30, 70, 40, 50),
  5.                   site = rep(c("site_A", "site_B"), 3))
  6. barchart(count ~ fruit, groups = site, dat,
  7.          auto.key = list(space = "right"))
  8.  
  9. dat2 <- group_by(dat, fruit) %>% summarise(count = sum(count)) %>%
  10.   mutate(ones = 1)
  11. barchart(count ~ ones, groups = fruit, dat2, stack = TRUE,
  12.          auto.key = list(space = "right"), horizontal = FALSE)
  13.  
  14. barchart( ~ count, groups = fruit, dat2, stack = TRUE,
  15.          auto.key = list(space = "right"))
  16.  
  17. library(latticeExtra)
  18. barchart(count ~ fruit, groups = site, dat,
  19.          auto.key = list(space = "right"),
  20.          par.settings = ggplot2like(n = nlevels(dat$site)),
  21.          axis = axis.grid)
  22.  
  23. barchart(count ~ ones, groups = fruit, dat2, stack = TRUE,
  24.          auto.key = list(space = "right"),
  25.          par.settings = ggplot2like(n = nlevels(dat$fruit)),
  26.          axis = axis.grid, horizontal = FALSE)
  27.  
  28. barchart( ~ count, groups = fruit, dat2, stack = TRUE,
  29.          auto.key = list(space = "right"),
  30.          par.settings = ggplot2like(n = nlevels(dat$fruit)),
  31.          axis = axis.grid)
  32.  
  33. library(ggplot2)
  34. ggplot(dat, aes(fruit, count, fill = site)) +
  35.   geom_bar(stat="identity", position="dodge")
  36.  
  37. ggplot(dat2, aes(ones, count, fill = fruit)) +
  38.   geom_bar(stat="identity", position="stack")
Advertisement
Add Comment
Please, Sign In to add comment