【问题标题】:add segments to scatter-plot将段添加到散点图
【发布时间】:2012-05-18 22:03:07
【问题描述】:

(这遵循ggplot2 loess Q,我得到了一个很好的答案)——导致这个情节:

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

我使用表 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 处绘制一个彩色线段来说明这一事实

我将这两种数据类型合并到一个带有标签列#10='type' 的表中(散点数据 ='cnv' 和 no-data='nregion' 的内容)。 nregions 在 value 列中为 0。

我怎样才能只用'cnv'数据作为散点图,只用'nregion'数据来绘制线段;都在同一个情节上?

我找到了 geom_segment:

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

但我没有找到为每个 ggplot 子图进行子集化的方法。

谢谢

#### 跟进@gauden 解决方案

嗨@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)

【问题讨论】:

    标签: r overlay ggplot2 segment scatter


    【解决方案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 的使用在对a supplementary question 的回答中进一步解释

    【讨论】:

    • 这正是我正在搜索的内容,非常感谢@gauden。我现在只需要了解 R 语法并将其转换为我自己的数据。大力支持!!
    • 亲爱的@gauden,我在顶部帖子中添加了更多信息,并带有指向新图片的链接。 (PS:我如何在 Stackoverflow 上打勾?我没有足够的积分来投票
    • 你是个巫师!!我也很钦佩其他几个人回答您的补充问题的协作努力(我怎样才能最好地奖励你们所有人?)。现在这完全符合我的需求。我意识到我在 R 中还有很长的路要走,只有基本的 perl 知识,在这里不容易翻译。向量结构对我的生物学家来说是违反直觉的,我总是不得不努力得到它。无论如何,我祝你有一个美好的一天,非常感谢你的帮助和教导。
    • 很高兴它对你有用。只要继续回答和提问 SO,这对所有人来说就足够了 :) 不久,我将在这里删除我的健谈 cmets 以减少混乱......
    猜你喜欢
    • 2018-11-12
    • 2019-05-08
    • 2020-08-27
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    相关资源
    最近更新 更多