-2

I want to plot both y1 and y2 against x.

I don't want to use ggplot2.

Is it possible with just the plot() function? I have tried using par(new = TRUE) but this literally overlays y2 with y1 and it looks messy. I want to color code it for both y1 and y2.

Grateful for any pointers. Thanks

sm925
  • 2,522
  • 1
  • 15
  • 22
Henry P.
  • 85
  • 5

1 Answers1

2

You can add variables with points() also you can use the col argument there. Check ?points() for more information. Here a small example with a costum dataset.

df<-mtcars
plot(df$disp, df$mpg)
points(df$hp, df$mpg, col="green")

Output:

plot

mischva11
  • 2,535
  • 3
  • 15
  • 31
  • This looks like exactly what I need! But how should I add a legend to describe which col relates to which variable? – Henry P. Jul 01 '19 at 13:40
  • This is such a basic concept in R that it's almost guaranteed to be a duplicate. Please do a search before you answer. – AkselA Jul 01 '19 at 13:43
  • 1
    @HenryP. take a look at [this](https://stackoverflow.com/questions/37401702/add-legend-to-a-plot-with-r) or ?legend – mischva11 Jul 01 '19 at 13:47