cs0815
cs0815

Reputation: 17388

combine data from 2 data frames in one plot using different graphs

I would like to combine a line plot and a scatter plot in one graph. The data comes from different data frames and the columns have different names. This is my reproducible example, which throws an error:

library(ggplot2)

x <- runif(1000, min = 0, max = 100)
y <- rnorm(1000, mean = 50, sd = 30)
df1 <- data.frame(
      x = x
      , y = y
)

x1 <- runif(10, min = 0, max = 100)
y1 <- rnorm(10, mean = 50, sd = 30)
df2 <- data.frame(
      x1 = x1
      , y1= y1
)

ggplot(df1, aes(x, y)) +
    geom_line() +
    geom_point(df2, aes(x1, y1))

Upvotes: 1

Views: 133

Answers (1)

Ulises Rosas-Puchuri
Ulises Rosas-Puchuri

Reputation: 1970

Try to upload data on each geom_* separately:

ggplot() +
  geom_line(data = df1, aes(x, y), color = "grey") +
  geom_point(data = df2, aes(x1, y1), color = "red") 

enter image description here

Upvotes: 2

Related Questions