Skip to content

Instantly share code, notes, and snippets.

@taylorreiter
Last active February 20, 2020 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taylorreiter/6a4a92b584e4e9aa122989a41d85c22b to your computer and use it in GitHub Desktop.
Save taylorreiter/6a4a92b584e4e9aa122989a41d85c22b to your computer and use it in GitHub Desktop.
################################################################################
## Intro to plotting with ggplot2: other goodies 2020-02-20
################################################################################
# Getting started ---------------------------------------------------------
# First let's download a data set called "portal_data_joined.csv"
# We'll put it in our data folder inside of our project directory.
# If you don't already have a data directory, you can create one in
# the files pane. Alternatively, you can download the data by hand:
# https://tinyurl.com/y36xgftg
download.file(url = "https://tinyurl.com/y36xgftg",
destfile = "data/portal_data_joined.csv")
install.packages("tidyverse")
library(tidyverse)
surveys <- read_csv("data/portal_data_joined.csv")
str(surveys)
View(surveys)
# use dplyr to remove NAs
surveys_complete <- surveys %>%
filter(!is.na(weight),
!is.na(hindfoot_length),
!is.na(sex))
# review: build a ggplot
ggplot(surveys_complete, aes(x = hindfoot_length, y = weight)) +
geom_point(alpha = .1) +
theme_minimal()
# titles, captions, and subtitles -----------------------------------------
# add a title
ggplot(surveys_complete, aes(x = hindfoot_length, y = weight)) +
geom_point(alpha = .1) +
theme_minimal() +
ggtitle("Hindfoot vs weight")
# add a subtitle
ggplot(surveys_complete, aes(x = hindfoot_length, y = weight)) +
geom_point(alpha = .1) +
theme_minimal() +
labs(title = "Hindfoot vs weight",
subtitle = "Surveys Data")
# add a caption
ggplot(surveys_complete, aes(x = hindfoot_length, y = weight)) +
geom_point(alpha = .1) +
theme_minimal() +
labs(title = "Hindfoot vs weight",
subtitle = "Surveys Data",
caption = "Data collected from 1977 - 2002")
# captions and titles are scriptable
ggplot(surveys_complete, aes(x = hindfoot_length, y = weight)) +
geom_point(alpha = .1) +
theme_minimal() +
labs(title = "Hindfoot vs weight",
subtitle = "Surveys Data",
caption = paste0("Data collected from ",
min(surveys_complete$year),
"-",
max(surveys_complete$year)))
# labeling points: ggrepel -----------------------------------------------
install.packages("ggrepel")
library(ggrepel)
ggplot(surveys_complete, aes(x = hindfoot_length, y = weight, label = species_id)) +
geom_point(alpha = .1) +
theme_minimal() +
geom_text_repel()
# only label species codes for outliers
ggplot(surveys_complete, aes(x = hindfoot_length, y = weight)) +
geom_point(alpha = .1) +
theme_minimal() +
geom_text_repel(data = filter(surveys_complete, hindfoot_length > 60),
aes(x = hindfoot_length, y = weight),
label = species_id)
# make interactive plots --------------------------------------------------
install.packages("plotly")
library(plotly)
plt <- ggplot(surveys_complete, aes(x = hindfoot_length, y = weight, label = species_id)) +
geom_point(alpha = .1) +
theme_minimal()
ggplotly(plt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment