2.8 Geoms

The geom_point() geom gives a familiar scatterplot.

Other geoms include:

  • geom_smooth() which fits a smooth line to the data

    • check help to see geom_smooth’s arguments like method, se or span.
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth()

  • geom_boxplot() which generates a box-and-whisker plot

    • check help to see geom_boxplot’s arguments like outlier arguments, and coef which adjusts the whisker length.
ggplot(mpg, aes(drv, hwy)) +
  geom_boxplot()

  • consider boxplot variants like geom_jitter and geom_violin
ggplot(mpg, aes(drv, hwy)) +
  geom_jitter()

ggplot(mpg, aes(drv, hwy)) +
  geom_violin()

  • geom_histogram which generates a histogram and geom_freqpoly which generates a frequency polygon

  • check help to see geom_histogram’s arguments like position and binwidth.

ggplot(mpg, aes(hwy)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(mpg, aes(hwy)) +
  geom_freqpoly()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

  • geom_bar which generates a bar chart

  • check help to see geom_bar’s arguments like position and width

ggplot(diamonds, aes(cut)) +
  geom_bar()

This graph below uses displ for y in the aesthetic and uses the stat of identity so that it sums the total displacement for each manufacturer.

ggplot(mpg, aes(manufacturer, displ)) +
  geom_bar(stat = "identity")

This plot now shows the total displacement.

mpg %>% group_by(manufacturer) %>% summarize(sum(displ))
## # A tibble: 15 × 2
##    manufacturer `sum(displ)`
##    <chr>               <dbl>
##  1 audi                 45.8
##  2 chevrolet            96.2
##  3 dodge               162  
##  4 ford                113. 
##  5 honda                15.4
##  6 hyundai              34  
##  7 jeep                 36.6
##  8 land rover           17.2
##  9 lincoln              16.2
## 10 mercury              17.6
## 11 nissan               42.5
## 12 pontiac              19.8
## 13 subaru               34.4
## 14 toyota              100. 
## 15 volkswagen           60.9
  • geom_line and geom_path which generates a line chart or path chart (useful for time series data)
    • check help to see geom_line’s arguments like lineend and arrow
ggplot(economics, aes(date, unemploy / pop)) +
  geom_line()

ggplot(economics, aes(date, uempmed)) +
  geom_line()

  • To investigate these plots further, we can draw them on the same plot.
year <- function(x) as.POSIXlt(x)$year + 1900


ggplot(economics, aes(unemploy / pop, uempmed)) +
  geom_path(color = "grey50") +
  geom_point(aes(color = year(date)))