The key to using any of the scale_ functions is to know what sort of data you're working with (e.g., date, continuous, discrete). Yesterday, I talked about scale_x_date and scale_x_discrete. We often put these types of data on the x-axis, while the y-axis is frequently used for counts. When displaying counts, we want to think about the major breaks that make sense, as well as any additional formatting to make them easier to read.
If I go back to my pages over time plot, you'll notice the major breaks are in the tens of thousands. We're generally used to seeing those values with a comma separating the thousands from the hundreds. I could add those to my plot like this (with a little help from the scales package).
library(tidyverse)
reads2019 <- read_csv("~/Downloads/Blogging A to Z/SaraReads2019_allchanges.csv", col_names = TRUE)
reads2019 <- reads2019 %>% mutate(date_started = as.Date(reads2019$date_started, format = '%m/%d/%Y'), date_read = as.Date(date_read, format = '%m/%d/%Y'), PagesRead = order_by(date_read, cumsum(Pages))) library(scales)
reads2019 %>% ggplot(aes(date_read, PagesRead)) + geom_point() + scale_x_date(date_labels = "%B", date_breaks = "1 month") + scale_y_continuous(labels = comma) + labs(title = "Cumulative Pages Read Over 2019") + theme(plot.title = element_text(hjust = 0.5))
reads2019 %>% ggplot(aes(date_read, PagesRead)) + geom_point() + scale_x_date(date_labels = "%B", date_breaks = "1 month") + scale_y_continuous(labels = comma, breaks = seq(0, 30000, 5000)) + labs(title = "Cumulative Pages Read Over 2019") + theme(plot.title = element_text(hjust = 0.5))
Last post tomorrow!
No comments:
Post a Comment