Some more coffee break data viz.
Demonstration of ggplot2 2D density plots and the viridis package
Credit to DataCamp for visualization ideas and educational resources.
Data: https://vincentarelbundock.github.io/Rdatasets/doc/datasets/faithful.html
library(tidyverse)
faithful <- read_csv('faithful.csv')
faithful## # A tibble: 272 x 3
## X1 eruptions waiting
## <int> <dbl> <int>
## 1 1 3.60 79
## 2 2 1.80 54
## 3 3 3.33 74
## 4 4 2.28 62
## 5 5 4.53 85
## 6 6 2.88 55
## 7 7 4.70 88
## 8 8 3.60 85
## 9 9 1.95 51
## 10 10 4.35 85
## # ... with 262 more rows
Get rid of the unnecessary X1 column and rename the observation columns for clarity
faithful_tidy <- faithful %>%
select(eruptions, waiting)
names(faithful_tidy) <- c('Eruption_Duration', 'Waiting_Time')
faithful_tidy## # A tibble: 272 x 2
## Eruption_Duration Waiting_Time
## <dbl> <int>
## 1 3.60 79
## 2 1.80 54
## 3 3.33 74
## 4 2.28 62
## 5 4.53 85
## 6 2.88 55
## 7 4.70 88
## 8 3.60 85
## 9 1.95 51
## 10 4.35 85
## # ... with 262 more rows
Set up a plot for eruption duration described by the amount of time until the next eruption
faithful_density <- faithful_tidy %>% ggplot(aes(x = Waiting_Time, y = Eruption_Duration)) +
scale_y_continuous(limits = c(1, 5.5), expand = c(0, 0)) +
scale_x_continuous(limits = c(40, 100), expand = c(0, 0)) +
coord_fixed(60 / 4.5)The lag time between eruptions plotted against the duration of the eruption can be represented nicely as a density plot. However, not all visualizations are created equal..
#Simply as a scatter plot
scatter_plot <- faithful_density +
geom_point() +
ggtitle('Simple Scatter')
#Simple density plot
density_simple <- faithful_density +
stat_density_2d(aes(col = ..level..), h = c(5, 0.5)) +
ggtitle('Simple Density')
#And with the standard viridis color scale
library(viridis)
density_viridis <- faithful_density +
stat_density_2d(geom = "tile", aes(fill = ..density..), h=c(5,.5), contour = FALSE) +
scale_fill_viridis() +
ggtitle('Density - Viridis Fill')Arranging multiple plots for comparison with grid.arrange
library(grid)
library(gridExtra)
library(lattice)
#Plus a caption
caption_text <- 'Old Faithful Data:
http://www.stat.cmu.edu/~larry/
all-of-statistics/=data/faithful.dat
--
All times in minutes'
caption <- textGrob(label = caption_text,
gp = gpar(fontface='italic')
)
grid.arrange(scatter_plot, density_simple, density_viridis, caption, ncol=2)