Creating a ggplot legend from two separate data frames

Hello,
I am creating graphs that contain data from two spearate data frames, and cannot seem to figure out how to add a separate legend for the second data frame. FJCC has been SUPER helpful getting me to this point, but I think this may warrant another topic.

My code:

  • I have two data frames (df, and df2)
  • df has a list of lakes and dates and parameters
  • df2 has a list of geometric mean values by years for those parameters and a column labelled "Legend" with "Geometric Mean" in each cell.
graph2 <- df1 %>%
  filter(parm =="tp") %>% 
  ggplot() +
  geom_point(mapping =aes(x=date, y=value, color=source)) +
  geom_point(mapping = aes(MyDate, GMean), data = df2, size = 1, color = "red") +
  geom_line(mapping = aes(MyDate, GMean), data = df2, size = 1, color = "purple") +
  theme(legend.position = "bottom") +
  facet_wrap(~lake, ncol=1, scales= "free", labeller = labeller(lake=lak.labs))
#> Error in df1 %>% filter(parm == "tp") %>% ggplot(): could not find function "%>%"
graph2 
#> Error in eval(expr, envir, enclos): object 'graph2' not found

Created on 2020-11-16 by the reprex package (v0.3.0)

I can create a plot with all the information, I just can figure out how to create a legend for the purple line:

I then tried the hack suggested to me by putting: color =legend within the df2 aes, but then the other legends are incorrect: 1) legend shows points and lines, where they are just points, and 2) I tried changing order, but they are in different data frames, so cant change order within the dataframe.

Thanks

Here is one attempt. I took the data from your other post and made a lake2 set by simply copying lake1 and changing the label.

library(lubridate)
library(tibble)
library(dplyr)
library(ggplot2)
tpexample <- tibble::tribble(
  ~lake,     ~source,       ~date, ~parm, ~value,  ~unit,
  "lake1", "lakewatch", "30-Jul-92",  "tp",   0.06, "mg/L",
  "lake1", "lakewatch", "18-Aug-92",  "tp",   0.07, "mg/L",
  "lake1", "lakewatch", "29-Sep-92",  "tp",   0.13, "mg/L",
  "lake1", "lakewatch", "29-Oct-92",  "tp",    0.1, "mg/L",
  "lake1", "lakewatch", "16-Nov-92",  "tp",   0.16, "mg/L",
  "lake1", "lakewatch", "16-Dec-92",  "tp",   0.13, "mg/L",
  "lake1", "lakewatch", "19-Jan-93",  "tp",   0.09, "mg/L",
  "lake1", "lakewatch", "10-Feb-93",  "tp",   0.09, "mg/L",
  "lake1", "lakewatch", "24-Mar-93",  "tp",   0.05, "mg/L",
  "lake1", "lakewatch", "28-Apr-93",  "tp",   0.05, "mg/L",
  "lake1", "lakewatch", "28-May-93",  "tp",   0.04, "mg/L",
  "lake1", "lakewatch", "29-Jun-93",  "tp",   0.04, "mg/L",
  "lake1", "lakewatch", "29-Jul-93",  "tp",   0.04, "mg/L",
  "lake1", "lakewatch", "20-Aug-93",  "tp",   0.03, "mg/L",
  "lake1", "lakewatch", "27-Sep-93",  "tp",   0.03, "mg/L",
  "lake1", "lakewatch", "20-Oct-93",  "tp",   0.04, "mg/L",
  "lake1", "lakewatch", "22-Nov-93",  "tp",   0.04, "mg/L",
  "lake1", "lakewatch", "22-Dec-93",  "tp",   0.05, "mg/L",
  "lake1", "lakewatch", "26-Jan-94",  "tp",   0.06, "mg/L",
  "lake1", "lakewatch", "23-Feb-94",  "tp",   0.05, "mg/L",
  "lake1", "lakewatch", "23-Mar-94",  "tp",   0.04, "mg/L",
  "lake1", "lakewatch", "27-Apr-94",  "tp",   0.03, "mg/L",
  "lake1", "lakewatch", "28-Jun-94",  "tp",   0.05, "mg/L",
  "lake1", "lakewatch",  "9-Jul-94",  "tp",   0.05, "mg/L",
  "lake1", "lakewatch",  "5-Oct-94",  "tp",   0.08, "mg/L",
  "lake1", "lakewatch",  "1-Nov-94",  "tp",   0.09, "mg/L",
  "lake1", "lakewatch", "22-Dec-94",  "tp",   0.11, "mg/L",
  "lake1", "lakewatch", "31-Jan-95",  "tp",    0.1, "mg/L",
  "lake1", "lakewatch", "16-Feb-95",  "tp",   0.08, "mg/L",
  "lake1", "lakewatch", "14-Mar-95",  "tp",   0.08, "mg/L",
  "lake1", "lakewatch", "13-Apr-95",  "tp",   0.06, "mg/L",
  "lake1", "lakewatch", "11-May-95",  "tp",   0.05, "mg/L",
  "lake1", "lakewatch", "20-Jun-95",  "tp",   0.03, "mg/L",
  "lake1", "lakewatch", "25-Jul-95",  "tp",   0.03, "mg/L",
  "lake1", "lakewatch", "21-Aug-95",  "tp",   0.17, "mg/L",
  "lake1", "lakewatch", "12-Sep-95",  "tp",   0.15, "mg/L",
  "lake1", "lakewatch", "16-Oct-95",  "tp",    0.1, "mg/L",
  "lake1", "lakewatch", "14-Nov-95",  "tp",    0.1, "mg/L",
  "lake1", "lakewatch", "12-Dec-95",  "tp",   0.06, "mg/L",
  "lake1", "lakewatch", "23-Jan-96",  "tp",   0.05, "mg/L",
  "lake1", "lakewatch", "15-Feb-96",  "tp",   0.07, "mg/L",
  "lake1", "lakewatch", "20-Mar-96",  "tp",   0.06, "mg/L",
  "lake2", "lakewatch", "30-Jul-92",  "tp",   0.06, "mg/L",
  "lake2", "lakewatch", "18-Aug-92",  "tp",   0.07, "mg/L",
  "lake2", "lakewatch", "29-Sep-92",  "tp",   0.13, "mg/L",
  "lake2", "lakewatch", "29-Oct-92",  "tp",    0.1, "mg/L",
  "lake2", "lakewatch", "16-Nov-92",  "tp",   0.16, "mg/L",
  "lake2", "lakewatch", "16-Dec-92",  "tp",   0.13, "mg/L",
  "lake2", "lakewatch", "19-Jan-93",  "tp",   0.09, "mg/L",
  "lake2", "lakewatch", "10-Feb-93",  "tp",   0.09, "mg/L",
  "lake2", "lakewatch", "24-Mar-93",  "tp",   0.05, "mg/L",
  "lake2", "lakewatch", "28-Apr-93",  "tp",   0.05, "mg/L",
  "lake2", "lakewatch", "28-May-93",  "tp",   0.04, "mg/L",
  "lake2", "lakewatch", "29-Jun-93",  "tp",   0.04, "mg/L",
  "lake2", "lakewatch", "29-Jul-93",  "tp",   0.04, "mg/L",
  "lake2", "lakewatch", "20-Aug-93",  "tp",   0.03, "mg/L",
  "lake2", "lakewatch", "27-Sep-93",  "tp",   0.03, "mg/L",
  "lake2", "lakewatch", "20-Oct-93",  "tp",   0.04, "mg/L",
  "lake2", "lakewatch", "22-Nov-93",  "tp",   0.04, "mg/L",
  "lake2", "lakewatch", "22-Dec-93",  "tp",   0.05, "mg/L",
  "lake2", "lakewatch", "26-Jan-94",  "tp",   0.06, "mg/L",
  "lake2", "lakewatch", "23-Feb-94",  "tp",   0.05, "mg/L",
  "lake2", "lakewatch", "23-Mar-94",  "tp",   0.04, "mg/L",
  "lake2", "lakewatch", "27-Apr-94",  "tp",   0.03, "mg/L",
  "lake2", "lakewatch", "28-Jun-94",  "tp",   0.05, "mg/L",
  "lake2", "lakewatch",  "9-Jul-94",  "tp",   0.05, "mg/L",
  "lake2", "lakewatch",  "5-Oct-94",  "tp",   0.08, "mg/L",
  "lake2", "lakewatch",  "1-Nov-94",  "tp",   0.09, "mg/L",
  "lake2", "lakewatch", "22-Dec-94",  "tp",   0.11, "mg/L",
  "lake2", "lakewatch", "31-Jan-95",  "tp",    0.1, "mg/L",
  "lake2", "lakewatch", "16-Feb-95",  "tp",   0.08, "mg/L",
  "lake2", "lakewatch", "14-Mar-95",  "tp",   0.08, "mg/L",
  "lake2", "lakewatch", "13-Apr-95",  "tp",   0.06, "mg/L",
  "lake2", "lakewatch", "11-May-95",  "tp",   0.05, "mg/L",
  "lake2", "lakewatch", "20-Jun-95",  "tp",   0.03, "mg/L",
  "lake2", "lakewatch", "25-Jul-95",  "tp",   0.03, "mg/L",
  "lake2", "lakewatch", "21-Aug-95",  "tp",   0.17, "mg/L",
  "lake2", "lakewatch", "12-Sep-95",  "tp",   0.15, "mg/L",
  "lake2", "lakewatch", "16-Oct-95",  "tp",    0.1, "mg/L",
  "lake2", "lakewatch", "14-Nov-95",  "tp",    0.1, "mg/L",
  "lake2", "lakewatch", "12-Dec-95",  "tp",   0.06, "mg/L",
  "lake2", "lakewatch", "23-Jan-96",  "tp",   0.05, "mg/L",
  "lake2", "lakewatch", "15-Feb-96",  "tp",   0.07, "mg/L",
  "lake2", "lakewatch", "20-Mar-96",  "tp",   0.06, "mg/L"
)

tpexample <- tpexample %>% mutate(date = dmy(date),
                                  year = year(date))
GeomMean2 <- tpexample %>% group_by(year) %>% 
  summarize(GMean = exp(mean(log(value))), MeanDate = mean(date)) %>% 
  mutate(GeomMean = " ") 

tpexample %>% ggplot() +
  geom_point(mapping =aes(x=date, y=value, color=source)) +
  geom_point(mapping = aes(MeanDate, GMean), data = GeomMean2, size = 1, color = "red") +
  geom_line(mapping = aes(MeanDate, GMean, linetype = GeomMean), data = GeomMean2, size = 1, color = "purple") +
  theme(legend.position = "bottom") +
  facet_wrap(~lake, ncol=1, scales= "free")

Created on 2020-11-16 by the reprex package (v0.2.1)

@FJCC--once again--thank you. that bit of code solved my problem!

Cheers.

Craig

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.