adam.888
adam.888

Reputation: 7846

Adding an extra line with less points using ggplot2

I am trying to add an extra line which contains less points. I have tried:

library(ggplot2)
df <- data.frame(x=c(1:50),y=c(1:50)*2+5)
df2 <-  data.frame(x=c(20,30,40),y=c(40,60,80))
plot1 <- ggplot(df , aes(x=x, y=y)) + geom_line()
plot2 <- plot1+ geom_line( aes(x=df2$x, y=df2$y))
plot2    

But this does not work.

Upvotes: 2

Views: 1790

Answers (1)

BenBarnes
BenBarnes

Reputation: 19454

You could pass df2 to the data argument of the second geom_line call and change the mapping slightly

plot2 <- plot1+ geom_line( aes(x=x, y=y), data = df2)

Upvotes: 3

Related Questions