Both ggplots below work separtely, but I would like to combine them to one plot where the Group
variable of the lines in df2
appears in the legend.
library(ggplot2)
df1 <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = rep(1:10,2),
y = c(seq(1,2,length.out = 10),
seq(5,6,length.out = 10)),
Group = c(rep("A",10),rep("B",10))
)
p1 <- ggplot(data = df1, aes(x = x, y = y))
geom_point()
p2<- ggplot(data = df2, aes(x = x, y = y,
group = Group, color = Group))
geom_line()
The problem is caused by the fact that 2 data frames have different column names. Therefore I cannot generate 2 plots and add them like p1 p2
as it is done in other solutions that have been published here before.
CodePudding user response:
First, in ggplot2
each layer has its own local data
argument, i.e. you could pass a different dataset to each layer. If you don't set the data
argument for a layer it will simply inherit the global dataset set in ggplot()
(if any), i.e. you could combine your plots into one by adding geom_line(data = df2, aes(group = Group, color = Group))
to your first plot.
Second, if your datasets have different column names that's not a big deal either. As with the data
argument, each layer has it's own (local) set of aesthetics. If not set, a layer will inherit the global aesthetics set in ggplot()
, i.e. with different column names you simply have to tell which columns should be mapped on the aesthetics in each layer.
Slightly altered your example data:
library(ggplot2)
df1 <- data.frame(x1 = 1:10, y1 = 1:10)
df2 <- data.frame(x2 = rep(1:10,2),
y2 = c(seq(1,2,length.out = 10),
seq(5,6,length.out = 10)),
Group = c(rep("A",10),rep("B",10))
)
ggplot(data = df1, aes(x = x1, y = y1))
geom_point()
geom_line(data = df2, aes(x = x2, y = y2, group = Group, color = Group))