Skip to content

Instantly share code, notes, and snippets.

@graebnerc
Created April 9, 2022 06:18
Show Gist options
  • Save graebnerc/8c35584881b87667e547a76d3342a194 to your computer and use it in GitHub Desktop.
Save graebnerc/8c35584881b87667e547a76d3342a194 to your computer and use it in GitHub Desktop.
Creating the descriptive violin plot for the beer data set, as used in the session on linear regression.
library(ggplot2)
library(tidyr)
library(dplyr)
library(DataScienceExercises) # https://github.com/graebnerc/DataScienceExercises/
beer_data <- DataScienceExercises::beer
# Original source: http://www.principlesofeconometrics.com/poe4/poe4stata.htm
beer_data_plot <- beer_data %>%
pivot_longer(
cols = everything(),
names_to = "variable",
values_to = "value") %>%
ggplot(
data = .,
aes(x=variable, y=value, fill=variable)
) +
geom_violin(# To draw a violin plot
draw_quantiles = c(0.25, 0.5, 0.75), # Show quantiles
alpha=0.5 # Transparency
) +
geom_jitter(# To draw non-overlapping points
alpha=0.5, size=0.5 # Transparency and smaller size
) +
scale_fill_brewer(palette = "Dark2") +
facet_wrap(~variable, # To get different sub-plots for each variable
ncol = 5, # Five columns
scales = "free" # Scales adjusted to each variable
) +
theme_icae() +
theme(
axis.title = element_blank(),
strip.text = element_blank(),
legend.position = "none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment