Adding a second line to a graph

I have three columns of data

  1. Sample, which provides details of whether the data was collected in the lab labelled L or in the review labelled R
  2. Year, which contains the year in which the leaf is sampled
  3. SD, which includes the stomatal density measured on the leaf

I am trying to add one line of SD over time of L samples and a second line of SD over time of R samples. Here is the code that I am trying to use but it is not working:

library(ggplot2)
ggplot(mydata4, aes(x=Year)) +
geom_line(aes(y=SD$Sample$L, color="Black")) +
geom_line(aes(y=SD$Sample$R, color="Grey")) +
labs(color="Legend text")

I would greatly appreciate any help anyone can offer. Thank you in advance.

The problem has been solved now, I used the following code.

p = ggplot() +
geom_smooth(data = mydata4, aes(x = Year, y = SD), color = "black") +
geom_smooth(data = mydata5, aes(x = Year, y = SD), color = "grey") +
xlab('Year') +
ylab('Stomatal Density (per mm squared)')

print(p)

There is a more elegant solution mapping the color aesthetic to the Sample variable, I'm going to give you an example using a built-in data frame (since you haven't provided sample data).

library(ggplot2)
    
    ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
        geom_smooth() +
        scale_color_manual(values = c("setoda" = "gray", "versicolor" = "black", "virginica" = "blue"))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2021-03-07 by the reprex package (v1.0.0.9002)

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

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.