将分段添加到散点图 [英] add segments to scatter-plot

查看:160
本文介绍了将分段添加到散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这遵循 ggplot2黄土问,我得到了一个很好的答案)-导致了该情节:

(this follows ggplot2 loess Q for which I got a nice answer) -- leading to this plot:

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

My R knowledge is quite limited (sorry!)

我使用表data1中的数据绘制散点图.

I plot a scatter using data from a table data1.

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处绘制一个彩色段以说明这一事实

Some regions have no-data (incl one big region in the middle but also smaller discrete regions) where I would like to draw a colored segments at y=0 to illustrate this fact

我将两种数据类型组合到一个表中,表的标签列为#10 ='type'(分散数据的内容='cnv'和无数据的内容='nregion'). n个区域的值列中为0.

I combined both data types into one table with a label column#10='type' (content for the scatter data ='cnv' and for the no-data='nregion'). nregions have 0 in the value column.

我怎样才能只获取散点图的"cnv"数据,而只能绘制"nregion"数据呢?两者都在同一个情节上?

How can I take only 'cnv' data for the scatter and only 'nregion' data to draw the segments; both on the same plot?

我找到了geom_segment:

I found geom_segment:

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

但是我没有找到为每个ggplot子图子集的方法.

BUT I did not find a way to subset for each ggplot sub-plot.

谢谢

你好@高登 我尝试了您的方法,但部分有效. 我的问题是我无法像使用] -1那样很好地划分数据. 0],因为我的n个区域是分散的(由图片中的蓝色点和线表示),并且对于每个新图都是不同的,如下图所示:

Hi @gauden I tried your approach and it partly worked. My problem is that I cannot divide my data as nicely as you do using ]-1; 0] because my nregions are scattered (represented by the blue dots and lines in the picture) and are different for each new graph, as in this image:

因此,黄土像以前一样穿过大的n地区.如何预防n地区的黄土?

Consequently, the loess goes through the large nregion as before. How can I prevent loess in 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)

推荐答案

完整的修订版本,以允许明确的请求

这是我的目标剧情:

Here is my target plot:

这是产生它的代码:

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

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆