R- How to draw a curve that crosses specific points

854 views Asked by At

I m new to stackoverflow, so please forgive if I m not clear enough. I have 2 groups of points each of which represent a different curve:

{(100,6.5),(200,6.2),(300,5.7),(400,5.5),(500,4.8)} , 
{(100,7),(200,6),(300,5.5),(400,5.3),(500,4.5)}

I want to draw these 2 curves in R in the same plot. I want the first curve to cross the first groups of points and the second curve to cross the second group of points. Does anybody have any idea?

1

There are 1 answers

1
Jake Kaupp On BEST ANSWER

If you were to use ggplot2, you'd construct your data frame like below, adding a variable that indicates the curve for each set of points, which we'll use to color the different lines. This is a simple example, as there is a lot of customization you can do with ggplot2

library(ggplot2)

df <- data.frame(x = rep(seq(100, 500, 100),2), y = c(6.5,6.2,5.7,5.5,4.8,7,6,5.5,5.3,4.5), curve = rep(c(1,2), each = 5))

ggplot(df, aes(x = x, y = y)) +
  geom_line(aes(color = factor(curve)))

enter image description here