consolidate ggplot examples in single dir
This commit is contained in:
98
ggplot_examples/crickets.r
Normal file
98
ggplot_examples/crickets.r
Normal file
@@ -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")
|
||||
29
ggplot_examples/mpg_plots.r
Normal file
29
ggplot_examples/mpg_plots.r
Normal file
@@ -0,0 +1,29 @@
|
||||
library(tidyverse)
|
||||
|
||||
# take the mpg data set and pipe it to the mutate function
|
||||
# then add a new column to the data set and save to mpg_metric
|
||||
mpg_metric <- mpg %>%
|
||||
mutate(city_metric = 0.425144 * cty)
|
||||
|
||||
# View the metric data set
|
||||
View(mpg_metric)
|
||||
|
||||
# group the mpg dataset by classes
|
||||
# then summarize the data by mean and median cty
|
||||
mpg %>%
|
||||
group_by(class) %>%
|
||||
summarise(mean(cty),
|
||||
median(cty))
|
||||
|
||||
# Data viz with ggplot2
|
||||
ggplot(mpg, aes(x = cty)) +
|
||||
geom_histogram() +
|
||||
labs(x = "City mileage")
|
||||
|
||||
# Scatter plot between city and highway mileage
|
||||
# this shows a linear relationship
|
||||
# then add a regression line on top
|
||||
ggplot(mpg, aes(x = cty,
|
||||
y = hwy)) +
|
||||
geom_point() +
|
||||
geom_smooth(method = "lm")
|
||||
Reference in New Issue
Block a user