You can specify the data
argument for each geom in your ggplot
call - consider the following code:
library(ggplot2)
# a built in dataset
first <- pressure
# a slight modification of the first df
second <- data.frame(temperature = first$temperature,
pressure = rev(first$pressure))
ggplot(mapping = aes(x = temperature, y = pressure)) +
geom_line(data = first, color = "red") +
geom_line(data = second, color = "blue")
The code is intentionally simplistic, but you can see that both lines are using different underlying data frames - red is on the first, blue on the second.
As the structure of both data frames remains the same I could get away with declaring the aes()
only once in the "root" ggplot call; if this were not the case you could always declare the aesthetics for each geom separately.
Note that when using separate data arguments you may need to tweak the range of your axes a bit; this will depend on your actual use case.