1

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))
5
  • 1
    Use geom_point(aes(x1, y1), df2). Layers (eg., geom_point) have different argument ordering than main ggplot call (ie, in layers aes goes first). Commented Feb 16, 2019 at 15:24
  • 1
    close vote?! common! Commented Feb 16, 2019 at 15:39
  • 1
    Isn't this a typo or why is this code not working? imo, you're passing wrong arguments. Commented Feb 16, 2019 at 15:40
  • 2 up votes and one close vote. whoever voted for close have some guts and elicit your reasons! Commented Feb 16, 2019 at 15:48
  • 1
    Possible duplicate of Overplotting from different data frames in ggplot2 Commented Feb 16, 2019 at 22:07

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.