library(tidyverse)
df1 <- tibble(wt=4, mpg=25)
df2 <- tibble(wt=2, mpg=15)
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_point(data=df1, aes(x=wt, y=mpg), color="red", size=3) +
geom_point(data=df2, aes(x=wt, y=mpg), color="blue", size=3)
Is there a way in ggplot that I can manually add a legend for the df1 and df2 data points as I have demo’d below (albeit using Preview)?
I can get a legend by binding the dataframes together …
df1 <- tibble(wt=4, mpg=25, source='My Car')
df2 <- tibble(wt=2, mpg=15, source='Your Car')
df <- bind_rows(mtcars, df1, df2)
ggplot(df, aes(x=wt, y=mpg, color = source)) +
geom_point()
… but I often find I want to manually add a legend, but am unsure of how to do so except in an outside tool like Google Slides.
3