Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
library(tidyverse)
library(ggstance) # For geom_pointrangeh()
library(forcats) # For factors
library(scales) # For nicer scales
set.seed(1234) # Make all random draws the same
example_data <- tibble(x1 = rnorm(10000),
x2 = rnorm(10000),
y1 = sample(1:100, 10000, replace = TRUE),
y2 = sample(LETTERS[1:4], 10000, replace = TRUE),
y3 = sample(LETTERS[10:11], 10000, replace = TRUE),
year = sample(2010:2017, 10000, replace = TRUE)) %>%
arrange(y2, year)
# write_csv(example_data, "data/example_data.csv")
First, I was interested in blah because blah, so I created a lollipop chart to show blah. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
example_data_summarized <- example_data %>%
group_by(y2, y3) %>%
summarize(n = n())
## `summarise()` has grouped output by 'y2'. You can override using the `.groups`
## argument.
figure1 <- ggplot(example_data_summarized,
aes(x = n, y = fct_rev(y2), color = y3)) +
geom_pointrangeh(aes(xmin = 0, xmax = n),
position = position_dodgev(height = 0.5),
size = 1, fatten = 5) +
labs(x = "Total number of things", y = NULL) +
guides(color = guide_legend(title = NULL)) +
scale_color_manual(values = c("#FF4266", "#82B09C")) +
theme(panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank())
figure1
## Warning: Using the `size` aesthetic with geom_segment was deprecated in ggplot2 3.4.0.
## ℹ Please use the `linewidth` aesthetic instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
ggsave(figure1, filename = "images/figure1.png",
width = 6, height = 4, units = "in", bg = "transparent")
Next, I wanted to see how things have changed over time, so I created a blah because blah. I found blah. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
example_data_time <- example_data %>%
gather(x_names, value, x1, x2) %>%
group_by(x_names, year, y2) %>%
summarize(x_avg = mean(value),
error = sd(value) / sqrt(length(value))) %>%
ungroup() %>%
mutate(upper = x_avg + (1.96 * error),
lower = x_avg - (1.96 * error)) %>%
mutate(x_names = recode(x_names,
x1 = "X1 (average)",
x2 = "X2 (average)"))
## `summarise()` has grouped output by 'x_names', 'year'. You can override using
## the `.groups` argument.
figure2 <- ggplot(example_data_time,
aes(x = year, y = x_avg, color = x_names)) +
geom_hline(yintercept = 0, size = 0.75,
color = "#CC3340", linetype = "dotted") +
geom_ribbon(aes(ymin = lower, ymax = upper,
fill = x_names, color = NULL), alpha = 0.2) +
geom_line(size = 1) +
scale_color_manual(values = c("#FA6900", "#69D1E8")) +
scale_y_continuous(labels = percent) +
guides(color = guide_legend(title = NULL), fill = FALSE) +
labs(x = NULL, y = "Whatever this is measuring") +
facet_wrap(~ y2, nrow = 1) +
theme(panel.grid.minor = element_blank())
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
figure2
ggsave(figure2, filename = "images/figure2.png",
width = 16, height = 3, units = "in", bg = "transparent")
I was also interested in the relationship between blah and blah, so I blahed. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
# There are a lot of points here and they're all random and pointless, so I
# simplify this graphic by just taking a subset of them
example_data_subset <- example_data %>%
sample_n(500)
figure3 <- ggplot(example_data_subset, aes(x = y1, y = x2, color = y2)) +
geom_point(size = 1, alpha = 0.75) +
geom_smooth(method = "lm", color = "#85144A", size = 2) +
labs(x = "Some variable", y = "Some other variable") +
guides(color = guide_legend(title = NULL)) +
scale_color_manual(values = c("#188146", "#004259", "#B00DC9", "#FFE01C")) +
facet_wrap(~ y3) +
theme(panel.grid.minor = element_blank())
figure3
## `geom_smooth()` using formula = 'y ~ x'
ggsave(figure3, filename = "images/figure3.png",
width = 6, height = 4, units = "in", bg = "transparent")
## `geom_smooth()` using formula = 'y ~ x'