Bar plots

Introduction to Data Visualization with ggplot2

Rick Scavetta

Founder, Scavetta Academy

Bar Plots, with a categorical X-axis

  • Use geom_bar() or geom_col()
Geom Stat Action
geom_bar() "count" Counts the number of cases at each x position
geom_col() "identity" Plot actual values
  • All positions from before are available
  • Two types
    • Absolute counts
    • Distributions
Introduction to Data Visualization with ggplot2

Bar Plots, with a categorical X-axis

  • Use geom_bar() or geom_col()
Geom Stat Action
geom_bar() "count" Counts the number of cases at each x position
geom_col() "identity" Plot actual values
Introduction to Data Visualization with ggplot2

Bar Plots, with a categorical X-axis

  • Use geom_bar() or geom_col()
Geom Stat Action
geom_bar() "count" Counts the number of cases at each x position
geom_col() "identity" Plot actual values
  • All positions from before are available
  • Two types
    • Absolute counts
    • Distributions
Introduction to Data Visualization with ggplot2

Habits of mammals

str(sleep)
'data.frame':    76 obs. of  3 variables:
 $ vore : Factor w/ 4 levels "carni","herbi",..: 1 4 2 4 2 2 1 1 2 2 ...
 $ total: num  12.1 17 14.4 14.9 4 14.4 8.7 10.1 3 5.3 ...
 $ rem  : num  NA 1.8 2.4 2.3 0.7 2.2 1.4 2.9 NA 0.6 ...
Introduction to Data Visualization with ggplot2

Bar plot

ggplot(sleep, aes(vore)) + 
  geom_bar()

Introduction to Data Visualization with ggplot2

Plotting distributions instead of absolute counts

# Calculate Descriptive Statistics:
iris %>% 
  select(Species, Sepal.Width) %>% 
  pivot_longer(!Species, names_to = "key", 
  values_to = "value") %>%
  group_by(Species) %>% 
  summarise(avg = mean(value), 
            stdev = sd(value))
   -> iris_summ_long
iris_summ_long
Species avg stdev
setosa 3.43 0.38
versicolor 2.77 0.31
virginica 2.97 0.32
Introduction to Data Visualization with ggplot2

Plotting distributions

ggplot(iris_summ_long, aes(x = Species, 
                           y = avg)) + 
  geom_col() +
  geom_errorbar(aes(ymin = avg - stdev, 
                    ymax = avg + stdev), 
                width = 0.1)

Introduction to Data Visualization with ggplot2

Let's practice!

Introduction to Data Visualization with ggplot2

Preparing Video For Download...