0

I am trying to plot two different datasets on the same plot. I am using this code to add the lines and to actually plot everything

ggplot()+
geom_point(data=Acc, aes(x=Year, y=Accumulo), color="lightskyblue")+
geom_line(data=Acc, aes(x=Year, y=RM3), color="gold1")+
geom_line(data=Acc, aes(x=Year, y=RM5), color="springgreen3")+
geom_line(data=Acc, aes(x=Year, y=RM50), color="blue")+
geom_line(data=Vulcani, aes(x=Year, y=Accumulo.V), color="red")+
theme_bw()+
scale_x_continuous(expand=expand_scale(0)) + scale_y_continuous(limits=c(50,350),expand=expand_scale(0))

but I can't find any way to add a legend and add custom labels to the different series. I find a way to add legends on a single dataset, but I can't find a way to add to this one a legend on the side

Raffaello Nardin
  • 151
  • 2
  • 11
  • 2
    a basic example for Acc and Vulcani would help us reproduce the problem – NicolasH2 Jan 21 '20 at 12:12
  • 1
    Looks like a duplicate [Adding legends to multiple line plots with ggplot](https://stackoverflow.com/questions/39201856/adding-legends-to-multiple-line-plots-with-ggplot) – tjebo Jan 21 '20 at 13:00
  • Feel free to add an extract of your datasets with `dput(Acc)` and `dput(Vulcani)` to your question to allow a more precise answer – Romain Jan 21 '20 at 13:52

1 Answers1

1

You are better off creating a single dataset tailored to your plot needs before, which would be in the long format, so that you can give a single geom_line() instruction, and add colors to the lines with aes(color = ...) within the call to geom_line(). Here's an example with the midwest dataset (consider them as distinct datasets for the sake of example)

library(ggplot2)
library(dplyr)
library(tidyr)

long_midwest <- midwest %>% 
    select(popwhite, popasian, PID, poptotal) %>% 
    gather(key = "variable", value = "value", -PID, -poptotal) # convert to long format

long_midwest2 <- midwest %>% 
    select(poptotal, perchsd, PID) %>% 
    gather(key = "variable", value = "value", -PID, -poptotal)

plot_data <- bind_rows(long_midwest, long_midwest2) %>% # bind datasets vertically
    mutate(line_type = ifelse(variable == 'perchsd', 'A', 'B')) # creates a line_type variable

ggplot(data = plot_data, aes(x=poptotal, y = value))+
    geom_line(aes(color = variable, linetype = line_type)) +
    scale_color_manual(
        values = c('lightskyblue', 'gold1', 'blue'),
        name = "My color legend"
    ) +
    scale_linetype_manual(
        values = c(3, 1), # play with the numbers to get the correct styling
        name = "My linetype legend"
    )

enter image description here

I added a line_type variable to show the most generic case where you want specific mapping between the column values and the line type. If it is the same than, say, variable, just use aes(color = variable, linetype = variable). You can then decide which linetype you want (see here for more details).

For customising the labels, just change the content of variable within the dataset with the desired values.

Romain
  • 1,931
  • 1
  • 13
  • 24
  • I am trying to do use this example as a reference, but as far as I understand, the dataset you are binding together are of the same length and will use the same x axis (correct me if I'm wrong)... I haven't specified this in my post bc I didn't think it was a key part of the solution, but the two dataset I have have two different series for the x axis (they are still plotted over the same variable, but with different values – Raffaello Nardin Jan 21 '20 at 13:46
  • `bind_rows` doesn't need to have common x values nor the same number of rows, it is a simple row binding. It does need columns from both datasets to have the same names, though. Just be sure that your x series from both datasets are of the same type (double or numeric I guess) – Romain Jan 21 '20 at 13:48
  • 1
    Ok, I'll try it! And yes, I need to have both dots and lines in the graph... when you say adding a line_type variable to the dataset ... what do you mean by it exactly? I'm sorry, but I am new to R – Raffaello Nardin Jan 21 '20 at 13:59
  • I've edited my answer to add a line type specification within the `aes()` call. Using `linetype` within an `aes()` call means you're mapping the style of the graph lines to the values of a variable in your dataset (that's what *is* an aesthetic in `ggplot2`). Also remember that differenciating the line types *should* have a meaning besides the visual impact and the meaning already given by colors, that's why I kept the second legend in the example plot. – Romain Jan 21 '20 at 14:11