[R] add a curve that fits the highest values on the plot.

Joshua Wiley jwiley.psych at gmail.com
Sun Jun 12 22:38:51 CEST 2011


Hi Nanami,

I am not sure exactly what you mean by "fits the peaks"...are any of
these plots what you want?

x <- 1:20
y <- c(19.4, 17.9, 8.1, 11.3, 7.8, 8, 5, 1.7, 3.9, 5.4, 7.5, 5.4,
4.7, 5, 4.9, 3.5, 2.9, 2.4, 1.4, 1.7)
## Find peaks
index <- which(c(NA, diff(sign(diff(y)))) == -2)

dev.new()
layout(matrix(c(1, 3, 2, 3), 2))
## Upper left plot
plot(x, y, xlim = range(x), ylim = range(y))
## Upper right plot
plot(x, y, type = "l")
## bottom plot (points)
plot(x, y)
## add the lines connecting the peaks
lines(x[index], y[index])

HTH,

Josh

On Sun, Jun 12, 2011 at 12:11 PM, ads pit
<deconstructed.morning at gmail.com> wrote:
> Hi everyone,
>
> I am given two vectors - for example -
>  x= c(0:20)
>> x
>  [1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
>
> and
>
>> y
>  [1] 19.4 17.9  8.1 11.3  7.8  8.0  5.0  1.7  3.9  5.4  7.5  5.4  4.7  5.0
>  4.9
> [16]  3.5 2.9 2.4 1.4 1.7
>
>> plot(x,y,xlim=range(x),ylim=range(y))
>
> How can I draw a curve that fits the peaks on the plot?
> Thank you :)
> Best,
> Nanami
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list