添加段散点图(add segments to scatter-plot)

2019-07-29 08:43发布

(此如下GGPLOT2黄土Q为此,我有一个很好的答案) -导致这一情节:

我的[R知识是非常有限的(对不起!)

我用从表中数据1数据绘制散布。

data1<-NaRV.omit(data[,c(2,3,7,10)]) #(2=start, 3=end, 7=value, 10=type)
ylabs='E / A - ratio'
p1<-ggplot(data1, aes(x=start, y=value)) +
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)

一些区域具有无数据(包括一个大的区域在中间,而且较小的离散的区域),其中,我想在y = 0来绘制染色段来说明这一事实

我结合两种数据类型与一个标签列#10 =“类型”一个表(内容散射数据=“CNV”和用于无数据=“NREGION”)。 nregions在值列有0。

我怎么能只拿“CNV”为散布数据,只有“NREGION”数据绘制段; 双方在同一个阴谋?

我发现geom_segment:

+ geom_segment(aes(x=data1$start, y=0, xend=data1$end, yend=0))

但我没有找到一个方法来子集每个ggplot次要情节。

谢谢

#### follow up on @gauden solution

嗨@gauden我想你的方法,它部分的工作。 我的问题是,我可以做的好,你不使用] -1分我的数据; 0]因为我nregions分散(由在图片中的蓝色点和线表示)和对于每个新的图不同,因为在此图像中:

因此,黄土像以前那样经过大NREGION。 如何防止在nregions黄土?

#############################
## plot settings (edit below)
spanv<-0.1
pointcol1="#E69F00"
pointcol2="#56B4E9"
pointcol3="#009E73"
points=20
onecol="green"
colnreg="blue"
xlabs=paste(onechr, " position", " (loess-span=", spanv, ")", sep="")

##### end edit ##############

########################################################
## using the center coordinate of each segment and points

## prepare plot #1
# plot E / A - ratio
## draw loess average for cnv
## draw line for nregion
ylabs='E / A - ratio'
p1<-ggplot(chrdata, aes(x=start+1000, y=E.R, group=type, label=type)) +
ylim(0,5) +
geom_hline(aes(yintercept=1, col=onecol)) +
geom_point(data = chrdata[chrdata$type != 'nregion',], shape=points, col=pointcol2) +
geom_smooth(data = chrdata[chrdata$type != 'nregion',], method="loess", span=spanv) +
geom_point(data = chrdata[chrdata$type == 'nregion',], col=colnreg) +
geom_segment(data = chrdata[chrdata$type == 'nregion',], aes(x=start, y=E.R, xend=end, yend=E.R), colour=colnreg, linetype=1, size=1) +
xlab(xlabs) +
ylab(ylabs)

Answer 1:

编辑:全面修订,允许明确要求

这是我的目标阴谋:

这里是产生它的代码:

library("ggplot2")

# CREATE DATA FRAME
# This is the sort of data that I understand you to have
start <- rnorm(200)
value <- rnorm(200) 
df <- data.frame( cbind(start, value) )
df[ df$start > -0.6 & df$start <= 0, "value"] <- 0
df[ df$start > -1.6 & df$start <= -1.3, "value"] <- 0
df[ df$start > 0.9 & df$start <= 1.2, "value"] <- 0

df$type <- rep('cnv', 200)
df[ df$value == 0, "type"] <- 'nregion'
df[ df$value != 0, "type"] <- 'cnv'

# SORT the data frame by value so that the 'cnv' and 
# 'nregion' chunks become contiguous
df <- df[order(start),]

# See note below. 
r <- rle(df$type)
df$label <- rep(seq(from=0, length=length(r$lengths)), times=r$lengths)

# set up plot with colour aesthetic to distinguish the three regions
# playing around with colour and group produces different effects
p <- ggplot(df, aes(x = start, 
                    y= value,
                    colour=type,
                    group = label)
            )
p <- p + theme_bw()
# draw points outside the 'nregion'
p <- p + geom_point( data = df[df$type != 'nregion',] )

# draw smoothed lines outside the 'nregion'
p <- p + geom_smooth( data = df[df$type != 'nregion',] )


# plot zero points inside the 'nregion' 
p <- p + geom_smooth( data = df[df$type == 'nregion',], size = 2 )
p

采用rle在回答中进一步解释补充质询



文章来源: add segments to scatter-plot