Reputation: 10629
I have the ctlns
list and I am trying to produce some visualization of the data
ctlns<-list(structure(list(level = 10, x = c(0.101666666666667, 0.06,
0.0385714285714286, 0.035, 0.035, 0.035, 0.04, 0.0433333333333333,
0.05, 0.0516666666666667, 0.06, 0.0606416584402764, 0.0606416584402764,
0.0766666666666667, 0.0766666666666667, 0.0933333333333333, 0.0933333333333333,
0.0975, 0.11, 0.110956351152526, 0.110956351152526, 0.135, 0.135
), y = c(0.01, 0.04125, 0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36,
0.41, 0.458123195380173, 0.46, 0.51, 0.56, 0.61, 0.66, 0.71,
0.76, 0.808123195380173, 0.81, 0.86, 0.91, 0.96)), .Names = c("level",
"x", "y")))
Then I,
plot(ctlns[[1]]$x,ctlns[[1]]$y, xlim=c(0,.21), ylim=c(0,1), lwd=2, type="l", col="darkred" )
And I get the plot
I would like to smooth the upper part of the red curve (y>0.2) while maintaining some of the curved structure (y<0.2)
lines(lowess(ctlns[[1]]$x,ctlns[[1]]$y,f=2/3), lwd=2, col="darkblue")
does a fine job for the former part but deletes the lower part of the curve. I have the following questions:
EDIT after discussion with agstudy
Because of of the curved nature of the red line, I was thinking what I need is probably a not a function smoothing y~x
but rather a graph function that connects the points x, y
with some kind of curved line. The points should be connected in order they appear within their vectors (x[1]
with y[1]
and so on...)
Is this possible?
Upvotes: 0
Views: 9511
Reputation: 49660
You probably want to use the xspline
function (or grid.xspline
if using grid graphics).
plot( ctlns[[1]], type='l', col='red' )
xspline( ctlns[[1]], shape=1, border='blue' )
You can do some pre smoothing of the data which might help some as well:
tmp.x <- ctlns[[1]]$x
tmp.y <- ctlns[[1]]$y
tmp <- cumsum( c(TRUE, head(tmp.x,-1) != tail(tmp.x,-1) ) )
tmp2.x <- tapply( tmp.x, tmp, mean )
tmp2.y <- tapply( tmp.y, tmp, mean )
xspline( tmp2.x, tmp2.y, shape=1, border='green' )
or using loess for the smoothing:
fit <- loess( tmp.y ~ tmp.x+tmp )
tmp3.y <- tapply( fitted(fit), tmp, mean )
xspline( tmp2.x, tmp3.y, shape=1, border='orange' )
Upvotes: 5
Reputation: 121608
to answer part 2 of your question:
lines(lowess(ctlns[[1]]$x[ctlns[[1]]$y<0.2],
ctlns[[1]]$y[ctlns[[1]]$y<0.2]), lwd=2, col="darkblue")
For the first part of your question , I guess that the algorithm is designed to work on function (mathematical defintion of the term) it removes the duplicates on x.
Edit after OP comment!
for me this is good , at least that I use LOESS function in an optimal manner. If you want to join all parts you create a small line for points that create problem.
ids <- duplicated(ctlns[[1]]$x) & ctlns[[1]]$y < 0.25
lines(ctlns[[1]]$x[ids],ctlns[[1]]$y[ids], lwd=4, col="darkblue")
Upvotes: 2