From c685d054c4e8fc7c3108fde38c351548085b1f28 Mon Sep 17 00:00:00 2001 From: aaron Date: Sat, 27 Jan 2024 13:31:34 +0100 Subject: [PATCH] add examples for the cricket dataset --- crickets.r | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 crickets.r diff --git a/crickets.r b/crickets.r new file mode 100644 index 0000000..40d3de4 --- /dev/null +++ b/crickets.r @@ -0,0 +1,98 @@ +library(tidyverse) +library(modeldata) + +# the dataset +View(crickets) + +# basic scatter plot +ggplot(crickets, aes(x = temp, + y = rate, + color = species)) + + geom_point() + + labs(x = "Termperature", + y = "Chirp Rate", + color = "Species", + title = "Cricket Chripts", + caption = "Source: McDonal (2009)") + + scale_color_brewer(palette = "Dark2") + +# modifying basic properties of the plot +ggplot(crickets, aes(x = temp, + y = rate)) + + geom_point(color = "red", + size = 2, + shape = "square") + + labs(x = "Termperature", + y = "Chirp Rate", + title = "Cricket Chripts", + caption = "Source: McDonal (2009)") + + scale_color_brewer(palette = "Dark2") + +# learn more about the options for the geom +# with ?geom_point + +# adding another layer like a regression line +ggplot(crickets, aes(x = temp, + y = rate)) + + geom_point() + + geom_smooth(method = "lm", + se = FALSE) + + labs(x = "Termperature", + y = "Chirp Rate", + title = "Cricket Chripts", + caption = "Source: McDonal (2009)") + + scale_color_brewer(palette = "Dark2") + +# adding another regression line per species +ggplot(crickets, aes(x = temp, + y = rate, + color = species)) + + geom_point() + + geom_smooth(method = "lm", + se = FALSE) + + labs(x = "Termperature", + y = "Chirp Rate", + title = "Cricket Chripts", + caption = "Source: McDonal (2009)") + + scale_color_brewer(palette = "Dark2") + + theme_light() + +# other plots like histograms and frequency poly +ggplot(crickets, aes(x = rate)) + + geom_histogram(bins = 15) # one quantitative variable + +# frequency polynome +ggplot(crickets, aes(x = rate)) + + geom_freqpoly(bins = 15) + +# bar plot with fill color according to species, no legend +ggplot(crickets, aes(x = species, + fill = species)) + + geom_bar(show.legend = FALSE) + + scale_fill_brewer(palette = "Dark2") + +# boxplot with color equal to species +ggplot(crickets, aes(x = species, + y = rate, + color = species)) + + geom_boxplot(show.legend = FALSE) + + scale_color_brewer(palette = "Dark2") + + theme_minimal() + +# facetign (side by side) +# show the differnce between species + +# not great: +ggplot(crickets, aes(x = rate, + fill = species)) + + geom_histogram(bins = 15) + + scale_fill_brewer(palette = "Dark2") + +# better style +ggplot(crickets, aes(x = rate, + fill = species)) + + geom_histogram(bins = 15, + show.legend = FALSE) + + facet_wrap(~species, + ncol = 1) + + scale_fill_brewer(palette = "Dark2")