ggplot2_2_chapter1.pdf

chapter 1

1.1 Stats with geoms

Two categories of functions:
  • Called from within a geom
  • Called independently
  1. p <- ggplot(iris, aes(x = Sepal.Width))
  2. p + geom_histogram()
  3. p + stat_bin()
  4. p <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(am)))
  5. p + geom_bar()
  6. p + stat_count()
PART7:Intermediate Data Visualization with ggplot2 - 图1 PART7:Intermediate Data Visualization with ggplot2 - 图2
  1. ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  2. geom_point() +
  3. geom_smooth()
  4. ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  5. geom_point() +
  6. geom_smooth(se = FALSE, span = 0.4)
  7. ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  8. geom_point() +
  9. geom_smooth(method = "lm", se = FALSE)
  10. ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  11. geom_point() +
  12. geom_smooth(method = "lm", fullrange = TRUE)
  1. # Amend the plot to add another smooth layer with dummy grouping
  2. ggplot(mtcars, aes(x = wt, y = mpg, color = fcyl)) +
  3. geom_point() +
  4. stat_smooth(method = "lm", se = FALSE) +
  5. stat_smooth(aes(group=1),method="lm",se=FALSE)
  6. # # Amend the plot
  7. ggplot(mtcars, aes(x = wt, y = mpg, color = fcyl)) +
  8. geom_point() +
  9. # Map color to dummy variable "All"
  10. stat_smooth(aes(color="All"),se = FALSE) +
  11. stat_smooth(method = "lm", se = FALS
PART7:Intermediate Data Visualization with ggplot2 - 图3 PART7:Intermediate Data Visualization with ggplot2 - 图4

PART7:Intermediate Data Visualization with ggplot2 - 图5

PART7:Intermediate Data Visualization with ggplot2 - 图6

1.2 Stats: sum and quantile

PART7:Intermediate Data Visualization with ggplot2 - 图7

  1. # Low precision (& integer) data
  2. p <- ggplot(iris, aes(Sepal.Length, Sepal.Width))
  3. p + geom_point()
  4. # Jittering may give a wrong impressions
  5. p + geom_jitter(alpha = 0.5, width = 0.1, height = 0.1)
  6. p + geom_count()
  7. p + stat_sum()
PART7:Intermediate Data Visualization with ggplot2 - 图8 PART7:Intermediate Data Visualization with ggplot2 - 图9 PART7:Intermediate Data Visualization with ggplot2 - 图10
  1. library(AER)
  2. data(Journals)
  3. p <- ggplot(Journals, aes(log(price/citations), log(subs))) +
  4. geom_point(alpha = 0.5)
  5. # Using geom_quantiles
  6. p + geom_quantile(quantiles = c(0.05, 0.50, 0.95))
Linear regression predicts the mean response from the explanatory variables, quantile regression predicts a quantile response (e.g. the median) from the explanatory variables.
PART7:Intermediate Data Visualization with ggplot2 - 图11 PART7:Intermediate Data Visualization with ggplot2 - 图12

PART7:Intermediate Data Visualization with ggplot2 - 图13