Skip to content

Commit bd1d5ff

Browse files
committed
hortened some lines of code to within 80 characters and added more whitespace where needed.
1 parent 5e45841 commit bd1d5ff

File tree

1 file changed

+80
-24
lines changed

1 file changed

+80
-24
lines changed

viz.Rmd

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ read_csv("data/mauna_loa.csv") |>
216216
mutate(date_measured = ym(date_measured)) |>
217217
select(-date_decimal) |>
218218
filter(ppm > 0, date_measured > date("1980/01/01")) |>
219-
#filter(ppm > 0, date_measured > interval(ymd("1980/01/01"), ymd("2021-01-01"))) |>
220219
write_csv("data/mauna_loa_data.csv")
221220
```
222221

@@ -291,6 +290,7 @@ knitr::include_graphics("img/ggplot_function_scatter.jpeg")
291290
```{r 03-data-co2-scatter, warning=FALSE, message=FALSE, fig.height = 4, fig.width = 6, fig.cap = "Scatter plot of atmospheric concentration of CO$_{2}$ over time"}
292291
co2_scatter <- ggplot(co2_df, aes(x = date_measured, y = ppm)) +
293292
geom_point()
293+
294294
co2_scatter
295295
```
296296

@@ -321,6 +321,7 @@ with just the default arguments:
321321
```{r 03-data-co2-line, warning=FALSE, message=FALSE, fig.cap = "Line plot of atmospheric concentration of CO$_{2}$ over time"}
322322
co2_line <- ggplot(co2_df, aes(x = date_measured, y = ppm)) +
323323
geom_line()
324+
324325
co2_line
325326
```
326327

@@ -393,6 +394,7 @@ co2_line <- ggplot(co2_df, aes(x = date_measured, y = ppm)) +
393394
ylab("Atmospheric CO2 (ppm)") +
394395
xlim(c(date("1990-01-01"), date("1993-12-01"))) +
395396
theme(text = element_text(size = 16))
397+
396398
co2_line
397399
```
398400

@@ -455,6 +457,7 @@ The result is shown in Figure \@ref(fig:03-data-faithful-scatter).
455457
```{r 03-data-faithful-scatter, warning=FALSE, message=FALSE, fig.cap = "Scatter plot of waiting time and eruption time"}
456458
faithful_scatter <- ggplot(faithful, aes(x = waiting, y = eruptions)) +
457459
geom_point()
460+
458461
faithful_scatter
459462
```
460463

@@ -470,6 +473,7 @@ faithful_scatter <- ggplot(faithful, aes(x = waiting, y = eruptions)) +
470473
geom_point() +
471474
labs(x = "Waiting Time (mins)", y = "Eruption Duration (mins)") +
472475
theme(text = element_text(size = 16))
476+
473477
faithful_scatter
474478
```
475479

@@ -517,12 +521,18 @@ ggplot(can_lang, aes(x = most_at_home, y = mother_tongue)) +
517521
y = "Mother tongue \n (number of Canadian residents)") +
518522
theme(text = element_text(size = 14))
519523
```
524+
520525
```{r mother-tongue-hidden-summaries, echo = FALSE, warning = FALSE, message = FALSE}
521526
numlang_speakers <- can_lang |>
522527
select(mother_tongue) |>
523-
summarize(maxsp = max(mother_tongue), minsp = min(mother_tongue))
524-
maxlang_speakers <- numlang_speakers |> pull(maxsp)
525-
minlang_speakers <- numlang_speakers |> pull(minsp)
528+
summarize(maxsp = max(mother_tongue),
529+
minsp = min(mother_tongue))
530+
531+
maxlang_speakers <- numlang_speakers |>
532+
pull(maxsp)
533+
534+
minlang_speakers <- numlang_speakers |>
535+
pull(minsp)
526536
```
527537

528538
Okay! The axes and labels in Figure \@ref(fig:03-mother-tongue-vs-most-at-home-labs) are
@@ -584,6 +594,7 @@ ggplot(can_lang, aes(x = most_at_home, y = mother_tongue)) +
584594
english_mother_tongue <- can_lang |>
585595
filter(language == "English") |>
586596
pull(mother_tongue)
597+
587598
census_popn <- 35151728
588599
```
589600

@@ -614,6 +625,7 @@ can_lang <- can_lang |>
614625
mother_tongue_percent = (mother_tongue / 35151728)*100,
615626
most_at_home_percent = (most_at_home / 35151728)*100
616627
)
628+
617629
can_lang |>
618630
select(mother_tongue_percent, most_at_home_percent)
619631
```
@@ -721,7 +733,9 @@ visual redundancy&mdash;i.e., conveying the same information with both scatter p
721733
further improve the clarity and accessibility of your visualization.
722734
723735
```{r scatter-color-by-category-palette, fig.width=7.75, fig.height=4, warning=FALSE, fig.cap = "Scatter plot of percentage of Canadians reporting a language as their mother tongue vs the primary language at home colored by language category with color-blind friendly colors"}
724-
ggplot(can_lang, aes(x = most_at_home_percent, y = mother_tongue_percent, color = category, shape = category)) +
736+
ggplot(can_lang, aes(x = most_at_home_percent,
737+
y = mother_tongue_percent,
738+
color = category, shape = category)) +
725739
geom_point() +
726740
labs(x = "Language spoken most at home \n (percentage of Canadian residents)",
727741
y = "Mother tongue \n (percentage of Canadian residents)") +
@@ -808,8 +822,11 @@ The `islands.csv` data set \index{Island landmasses} contains a list of Earth's
808822
islands_df <- read_csv("data/islands.csv")
809823
continents <- c("Africa", "Antarctica", "Asia", "Australia",
810824
"Europe", "North America", "South America")
825+
811826
islands_df <- mutate(islands_df,
812-
landmass_type = ifelse(landmass %in% continents, "Continent", "Other"))
827+
landmass_type = ifelse(landmass %in% continents,
828+
"Continent", "Other"))
829+
813830
write_csv(islands_df, "data/islands.csv")
814831
```
815832

@@ -838,6 +855,7 @@ shown in Figure \@ref(fig:03-data-islands-bar).
838855
```{r 03-data-islands-bar, warning=FALSE, message=FALSE, fig.cap = "Bar plot of all Earth's landmasses' size with squished labels"}
839856
islands_bar <- ggplot(islands_df, aes(x = landmass, y = size)) +
840857
geom_bar(stat = "identity")
858+
841859
islands_bar
842860
```
843861

@@ -857,6 +875,7 @@ swapping the `x` and `y` variables:
857875
islands_top12 <- slice_max(islands_df, order_by = size, n = 12)
858876
islands_bar <- ggplot(islands_top12, aes(x = size, y = landmass)) +
859877
geom_bar(stat = "identity")
878+
860879
islands_bar
861880
```
862881

@@ -903,6 +922,7 @@ islands_bar <- ggplot(islands_top12,
903922
geom_bar(stat = "identity") +
904923
labs(x = "Size (1000 square mi)", y = "Landmass", fill = "Type") +
905924
theme(text = element_text(size = 16))
925+
906926
islands_bar
907927
```
908928

@@ -961,6 +981,7 @@ let's use the default arguments just to see how things look.
961981
```{r 03-data-morley-hist, warning=FALSE, message=FALSE, fig.cap = "Histogram of Michelson's speed of light data"}
962982
morley_hist <- ggplot(morley, aes(x = Speed)) +
963983
geom_histogram()
984+
964985
morley_hist
965986
```
966987

@@ -991,6 +1012,7 @@ while *horizontal lines* are used to denote quantities on the *vertical axis*.
9911012
morley_hist <- ggplot(morley, aes(x = Speed)) +
9921013
geom_histogram() +
9931014
geom_vline(xintercept = 792.458, linetype = "dashed", size = 1)
1015+
9941016
morley_hist
9951017
```
9961018

@@ -1019,6 +1041,7 @@ when they are colored by another categorical variable).
10191041
morley_hist <- ggplot(morley, aes(x = Speed, fill = Expt)) +
10201042
geom_histogram(alpha = 0.5, position = "identity") +
10211043
geom_vline(xintercept = 792.458, linetype = "dashed", size = 1.0)
1044+
10221045
morley_hist
10231046
```
10241047

@@ -1042,6 +1065,7 @@ and the color will be mapped discretely.
10421065
morley_hist <- ggplot(morley, aes(x = Speed, fill = as_factor(Expt))) +
10431066
geom_histogram(alpha = 0.5, position = "identity") +
10441067
geom_vline(xintercept = 792.458, linetype = "dashed", size = 1.0)
1068+
10451069
morley_hist
10461070
```
10471071

@@ -1081,6 +1105,7 @@ morley_hist <- ggplot(morley, aes(x = Speed, fill = as_factor(Expt))) +
10811105
geom_histogram() +
10821106
facet_grid(rows = vars(Expt)) +
10831107
geom_vline(xintercept = 792.458, linetype = "dashed", size = 1.0)
1108+
10841109
morley_hist
10851110
```
10861111

@@ -1101,13 +1126,21 @@ To answer this question, we'll use the `mutate` function to transform our data i
11011126
\index{ggplot!labs}\index{ggplot!theme}
11021127

11031128
```{r 03-data-morley-hist-5, warning=FALSE, message=FALSE, fig.height = 7, fig.cap = "Histogram of relative accuracy split vertically by experiment with clearer axes and labels"}
1104-
morley_rel <- mutate(morley, relative_accuracy = 100 * ((299000 + Speed) - 299792.458) / (299792.458))
1105-
morley_hist <- ggplot(morley_rel, aes(x = relative_accuracy, fill = as_factor(Expt))) +
1129+
morley_rel <- mutate(morley,
1130+
relative_accuracy = 100 *
1131+
((299000 + Speed) - 299792.458) / (299792.458))
1132+
1133+
morley_hist <- ggplot(morley_rel,
1134+
aes(x = relative_accuracy,
1135+
fill = as_factor(Expt))) +
11061136
geom_histogram() +
11071137
facet_grid(rows = vars(Expt)) +
11081138
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1109-
labs(x = "Relative Accuracy (%)", y = "# Measurements", fill = "Experiment ID") +
1139+
labs(x = "Relative Accuracy (%)",
1140+
y = "# Measurements",
1141+
fill = "Experiment ID") +
11101142
theme(text = element_text(size = 14))
1143+
11111144
morley_hist
11121145
```
11131146

@@ -1149,35 +1182,51 @@ and the binwidth of 0.01 are effective for helping answer our question.
11491182
On the other hand, the bin widths of 0.001 and 0.1 are too small and too big, respectively.
11501183

11511184
```{r 03-data-morley-hist-binwidth, echo = FALSE, warning = FALSE, message = FALSE, fig.height = 10, fig.cap = "Effect of varying bin width on histograms."}
1152-
morley_hist_default <- ggplot(morley_rel, aes(x = relative_accuracy, fill = as_factor(Expt))) +
1185+
morley_hist_default <- ggplot(morley_rel,
1186+
aes(x = relative_accuracy,
1187+
fill = as_factor(Expt))) +
11531188
geom_histogram() +
11541189
facet_grid(rows = vars(Expt)) +
11551190
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1156-
labs(x = "Relative Accuracy (%)", y = "# Measurements", fill = "Experiment ID") +
1191+
labs(x = "Relative Accuracy (%)",
1192+
y = "# Measurements",
1193+
fill = "Experiment ID") +
11571194
theme(legend.position = "none") +
11581195
ggtitle("Default bin width (bins = 30)")
11591196
1160-
morley_hist_big <- ggplot(morley_rel, aes(x = relative_accuracy, fill = as_factor(Expt))) +
1197+
morley_hist_big <- ggplot(morley_rel,
1198+
aes(x = relative_accuracy,
1199+
fill = as_factor(Expt))) +
11611200
geom_histogram(binwidth = 0.1) +
11621201
facet_grid(rows = vars(Expt)) +
11631202
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1164-
labs(x = "Relative Accuracy (%)", y = "# Measurements", fill = "Experiment ID") +
1203+
labs(x = "Relative Accuracy (%)",
1204+
y = "# Measurements",
1205+
fill = "Experiment ID") +
11651206
theme(legend.position = "none") +
11661207
ggtitle( "binwidth = 0.1")
11671208
1168-
morley_hist_med <- ggplot(morley_rel, aes(x = relative_accuracy, fill = as_factor(Expt))) +
1209+
morley_hist_med <- ggplot(morley_rel,
1210+
aes(x = relative_accuracy,
1211+
fill = as_factor(Expt))) +
11691212
geom_histogram(binwidth = 0.01) +
11701213
facet_grid(rows = vars(Expt)) +
11711214
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1172-
labs(x = "Relative Accuracy (%)", y = "# Measurements", fill = "Experiment ID") +
1215+
labs(x = "Relative Accuracy (%)",
1216+
y = "# Measurements",
1217+
fill = "Experiment ID") +
11731218
theme(legend.position = "none") +
11741219
ggtitle("binwidth = 0.01")
11751220
1176-
morley_hist_small <- ggplot(morley_rel, aes(x = relative_accuracy, fill = as_factor(Expt))) +
1221+
morley_hist_small <- ggplot(morley_rel,
1222+
aes(x = relative_accuracy,
1223+
fill = as_factor(Expt))) +
11771224
geom_histogram(binwidth = 0.001) +
11781225
facet_grid(rows = vars(Expt)) +
11791226
geom_vline(xintercept = 0, linetype = "dashed", size = 1.0) +
1180-
labs(x = "Relative Accuracy (%)", y = "# Measurements", fill = "Experiment ID") +
1227+
labs(x = "Relative Accuracy (%)",
1228+
y = "# Measurements",
1229+
fill = "Experiment ID") +
11811230
theme(legend.position = "none") +
11821231
ggtitle("binwidth = 0.001")
11831232
@@ -1200,7 +1249,8 @@ we can use the `+` operator to add a title layer with the `ggtitle` function.
12001249

12011250
```{r 03-data-morley-hist-addlayer, warning = FALSE, message = FALSE, fig.height = 7, fig.cap = "Histogram of relative accuracy split vertically by experiment with a descriptive title highlighting the take home message of the visualization."}
12021251
morley_hist_title <- morley_hist +
1203-
ggtitle("Michelson's speed of light experiments \n were accurate to about 0.05%")
1252+
ggtitle("Speed of light experiments \n were accurate to about 0.05%")
1253+
12041254
morley_hist_title
12051255
```
12061256

@@ -1369,13 +1419,19 @@ file_sizes <- tibble(`Image type` = c("Bitmap / Raster",
13691419
"Bitmap / Raster",
13701420
"Vector / Scalable Graphics"),
13711421
`File type` = c("PNG", "JPG", "BMP", "TIFF", "SVG"),
1372-
`Image size` = c(paste(round(file.info("img/faithful_plot.png")["size"] / 1000000, 2), "MB"),
1373-
paste(round(file.info("img/faithful_plot.jpg")["size"] / 1000000, 2), "MB"),
1374-
paste(round(file.info("img/faithful_plot.bmp")["size"] / 1000000, 2), "MB"),
1375-
paste(round(file.info("img/faithful_plot.tiff")["size"] / 1000000, 2), "MB"),
1376-
paste(round(file.info("img/faithful_plot.svg")["size"] / 1000000, 2), "MB")))
1422+
`Image size` = c(paste(round(file.info("img/faithful_plot.png")["size"]
1423+
/ 1000000, 2), "MB"),
1424+
paste(round(file.info("img/faithful_plot.jpg")["size"]
1425+
/ 1000000, 2), "MB"),
1426+
paste(round(file.info("img/faithful_plot.bmp")["size"]
1427+
/ 1000000, 2), "MB"),
1428+
paste(round(file.info("img/faithful_plot.tiff")["size"]
1429+
/ 1000000, 2), "MB"),
1430+
paste(round(file.info("img/faithful_plot.svg")["size"]
1431+
/ 1000000, 2), "MB")))
13771432
kable(file_sizes,
1378-
caption = "File sizes of `faithful_plot` when saved as different file formats.")
1433+
caption = paste0("File sizes of `faithful_plot`",
1434+
"when saved as different file formats."))
13791435
```
13801436

13811437
Take a look at the file sizes in Table \@ref(tab:filesizes).

0 commit comments

Comments
 (0)