Total beginner needs help combining plots

I am a total beginner using R for a university project
I need to create a line plot using two datasets, and have two lines from each data set.
right now i have two plots, for which i do not know how to add a legend, and i do not know how to use na.rm to ignore missing data points. eventually i will need to combine these two plots into one if possible

here is my current code.

file = "AA1 Log.R"

library(ggplot2)
require(ggplot2)

CA=(NTN_ca45_a_s_mgl)
PA=(NTN_pa29_a_s_mgl)

###scatter plot of SO4 and NO3 over time for PA###

ggplot(data= PA, aes(x=yr)) +

geom_line(aes(y = SO4), color = "red") + 
geom_line(aes(y = NO3), color = "blue") +

labs(y='Compound concentration',x='Year') +
theme_classic() +
ggtitle("Concentration of SO4 and NO3 over time in Pennsylvania")

###scatter plot of SO4 and NO3 over time for CA###

ggplot(data= CA, aes(x=yr), color= y)+
geom_point(aes(y = SO4), color = "orange") +
geom_path(aes(y = SO4), color = "orange") +
geom_point(aes(y = NO3), color = "green") +
geom_path(aes(y = NO3), color = "green") +
labs(y='Compound concentration',x='Year',
title = "Concentration of SO4 and NO3 over time in California", ) +
theme_classic() +

any help or tips is greatly appreciated!

Hi @parasarker , first you need put an example of his data, use dput() function and check the reproducible example policy.

This link help you for guidence in the multiples plots or combining plots .

ggplot2 - Easy Way to Mix Multiple Graphs on The Same Page - Articles - STHDA

Just to expand @M_AcostaCH 's point, we need a reproducible example (reprex)

aAd sample data. A handy way to supply sample data is to use the dput() function. See ?dput. If you have a very large data set then something like head(dput(myfile), 100) will likely supply enough data for us to work with.

1 Like

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.