问题
I plot here values over length for a chromosome
The middle region without points contains no data and should not get a loess line. How can I modify my code to stop the loess line over this region? The data is continuous but I could add lines to mark the blank region with some special value or add a column with a label?? but how to use this in the command?
my current command:
library(IDPmisc)
# plot settings (edit here)
spanv<-0.05
pointcol1="#E69F00"
pointcol2="#56B4E9"
pointcol3="#009E73"
points=20
linecol="green"
xlabs=paste(onechr, " position", " (loess-span=", spanv, ")", sep="")
data1<-NaRV.omit(data[,c(2,7)]) # keep only x and y for the relevant data
# and clean NA and Inf
ylabs='E / A - ratio'
p1<-ggplot(data1, aes(x=start, y=E.R)) +
ylim(0,5) +
geom_point(shape=points, col=pointcol1, na.rm=T) +
geom_hline(aes(yintercept=1, col=linecol)) +
geom_smooth(method="loess", span=spanv, fullrange=F, se=T, na.rm=T) +
xlab(xlabs) +
ylab(ylabs)
回答1:
I would do one of two things:
- Do the
loess()
fitting outside ofggplot()
, predict for the two regions separately and add each set of predictions to the plot with its owngeom_line()
layer. - Similar to the above, but this time within
ggplot()
realm of operations. Add two layers to the plot, not one, both usinggeom_smooth()
, but importantly change thedata
argument supplied to each to refer to just one or the other portion of data.
For the latter, perhaps something like:
....
geom_smooth(data = data[1:n, ], method="loess", span=spanv, fullrange=FALSE,
se=TRUE, na.rm=TRUE) +
geom_smooth(data = data[m:k, ], method="loess", span=spanv, fullrange=FALSE,
se=TRUE, na.rm=TRUE)
....
where n
and m
and k
refer to the indices that mark the end of set 1 and the start and end of set 2 and which need to be defined or supplied by you directly.
来源:https://stackoverflow.com/questions/10530036/scatter-plot-with-loess-line-loess-to-not-show-line-in-a-given-region