library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
surveys_complete <- read_csv("generated_data/surveys_complete.csv")
## Parsed with column specification:
## cols(
## record_id = col_double(),
## month = col_double(),
## day = col_double(),
## year = col_double(),
## plot_id = col_double(),
## species_id = col_character(),
## sex = col_character(),
## hindfoot_length = col_double(),
## weight = col_double(),
## genus = col_character(),
## species = col_character(),
## taxa = col_character(),
## plot_type = col_character()
## )
# template: ggplot(data = <DATA>, mapping = aes(<MAPPINGS>)) + <GEOM_FUNCTION>()
# bind the plot to specific data
ggplot(data = surveys_complete)
# select the variables from the data you want to plot
ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length))
# add "geoms" (points, lines, bars, etc.); we'll use geom_point for continuous vars
ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
geom_point()
# Assign plot to a variable
surveys_plot <- ggplot(data = surveys_complete,
mapping = aes(x = weight, y = hindfoot_length))
# Draw the plot
surveys_plot +
geom_point()
# This is the correct syntax for adding layers
# surveys_plot +
# geom_point()
# This will not add the new layer and will return an error message
# surveys_plot
# + geom_point()
library("hexbin")
surveys_plot +
geom_hex()
# going back and modifying geom_point...
# add transparency
ggplot(data = surveys_complete, aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1)
# add colors
ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1, color = "blue")
# color for each species
ggplot(data = surveys_complete, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1, aes(color = species_id))
Challenge: Create a scatter plot of weight over species_id with the plot types showing in different colors. Is this a good way to show this type of data?
ggplot(data = surveys_complete,
mapping = aes(x = species_id, y = weight)) +
geom_point(aes(color = plot_type))
No, not good. Might be better if we switched plot_type to be on the x?
ggplot(data = surveys_complete,
mapping = aes(x = plot_type, y = weight)) +
geom_point(aes(color = species_id), alpha = 0.1)