Table of Contents


Visualizing data with ggplots

simple plots

> library(tinyverse)
> print(mpg, n=5)
# A tibble: 234 × 11
  manufacturer model displ  year   cyl trans      drv     cty   hwy fl    class  
  <chr>        <chr> <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>  
1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p     compact
2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p     compact
3 audi         a4      2    2008     4 manual(m6) f        20    31 p     compact
4 audi         a4      2    2008     4 auto(av)   f        21    30 p     compact
5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p     compact
 
> ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, color = drv))

img-20260819-140858.png

Facets

> ggplot(data = mpg) +
+     geom_point(mapping = aes(x = displ, y = hwy)) +
+     facet_wrap(~ class, nrow = 2)

img-20260819-140857.png

Geometric objects

> ggplot(data = mpg) +
+     geom_smooth(mapping = aes(x = displ, y = hwy, group=drv))

__smooth
img-20260819-140858-1.png

box plots

> ggplot(data = mpg) + geom_boxplot(mapping = aes(x= class, y = hw

img-20260820-200818.png

Density plot

> ggplot(data = mpg) + geom_density(mapping=aes(x=displ, alpha = 0.2, color=class, fill=class)) + theme(legend.position = "none")

img-20260820-200822.png

Statistical Transformations

> print(diamonds, n=3)
# A tibble: 53,940 × 10
  carat cut     color clarity depth table price     x     y     z
  <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1  0.23 Ideal   E     SI2      61.5    55   326  3.95  3.98  2.43
2  0.21 Premium E     SI1      59.8    61   326  3.89  3.84  2.31
3  0.23 Good    E     VS1      56.9    65   327  4.05  4.07  2.31
# ℹ 53,937 more rows
 
> summary(diamonds$cut)
     Fair      Good Very Good   Premium     Ideal 
     1610      4906     12082     13791     21551 
     
> ggplot(data = diamonds) +
	geom_bar(mapping = aes(x = cut))

img-20260819-140849.png

Statistics plot

> ggplot(data = diamonds) +
	stat_summary(
		mapping = aes(x = cut, y = depth),
		fun.min = min,
		fun.max = max,
		fun.y = median
	)

img-20260819-140864.png

Histograms

> ggplot(data=diamonds) + geom_histogram(aes(x=carat), bins=3) + facet_wrap(~cut)

img-20260819-150800.png

Boxplots

> ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
    geom_boxplot()

img-20260819-150864.png

Titles

ggplot(mpg, aes(displ, hwy)) + 
  geom_point(aes(colour = factor(cyl))) + 
  labs(
    x = "Engine displacement (litres)", 
    y = "Highway miles per gallon", 
    colour = "Number of cylinders",
    title = "Mileage by engine size and cylinders",
    subtitle = "Source: https://fueleconomy.gov"
  )

img-20260820-200817.png

Laying out plots side by side

p1 <- ggplot(mpg) + 
  geom_point(aes(x = displ, y = hwy))
 
p2 <- ggplot(mpg) + 
  geom_bar(aes(x = as.character(year), fill = drv), position = "dodge") + 
  labs(x = "year")
 
p3 <- ggplot(mpg) + 
  geom_density(aes(x = hwy, fill = drv), colour = NA) + 
  facet_grid(rows = vars(drv))
 
p4 <- ggplot(mpg) + 
  stat_summary(aes(x = drv, y = hwy, fill = drv), geom = "col", fun.data = mean_se) +
  stat_summary(aes(x = drv, y = hwy), geom = "errorbar", fun.data = mean_se, width = 0.5)
  
  
library(patchwork)
 
p1 | p2

img-20260820-200832.png

p1 / p2

img-20260820-200821.png

p12 <- p1 + inset_element(p2, left = 0.5, bottom = 0.5, right = 0.9, top = 0.95)
p12 & theme_bw()

img-20260820-200833.png