scatter plot with loess line, loess to not show line in a given region

独自空忆成欢 提交于 2019-12-11 10:43:52

问题


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:

  1. Do the loess() fitting outside of ggplot(), predict for the two regions separately and add each set of predictions to the plot with its own geom_line() layer.
  2. Similar to the above, but this time within ggplot() realm of operations. Add two layers to the plot, not one, both using geom_smooth(), but importantly change the data 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!