I've got a data frame that looks like this:
glimpse(spottingIntensityByMonth)
# Observations: 27
# Variables: 3
# $ yearMonth <dttm> 2015-05-01, 2015-06-01, 2015-07-01, 2015-08-01, 2015-09-01, 2015-10-01, 2...
# $ nClassificationsPerDayPerSpotter <dbl> 3.322581, 13.212500, 13.621701,
6.194700, 18.127778, 12.539589, 8.659722, ...
# $ nSpotters <int> 8, 8, 22, 28, 24, 22, 24, 27, 25, 29, 32, 32, 21, 14, 18, 13, 20, 19, 15, ...
I am trying to plot it with ggplot2 like so:
ggplot() +
geom_col(data = spottingIntensityByMonth,
mapping = aes(x = yearMonth,
y = nClassificationsPerDayPerSpotter)
) +
xlab("Month of year") +
scale_y_continuous(name = "Daily classifications per Spotter") +
geom_line(data = spottingIntensityByMonth,
mapping = aes(x = yearMonth,
y = nSpotters)
) +
theme_bw()
This produces a plot like so:
Now I want to add a legend that says what the line and columns mean. How do I do this? Thanks!
library(xlsx) # Library for Excel import library (ggplot2) # Library for plot library(openxlsx) #FOR Running the xlsx file library(shiny) #SERVER UI LIBRARY IN R library(lubridate) # For converting the imported dates to Date Format
Output Curve
In ggplot, legends are automatically created for mapped aesthetics. You can add such mappings as follows:
In the above example, I've also moved the data & common aesthetic mapping (x) to
ggplot()
.Sample dataset: