99 lines
2.7 KiB
R
99 lines
2.7 KiB
R
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")
|