首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >geom_segment箭头方向

geom_segment箭头方向
EN

Stack Overflow用户
提问于 2017-01-26 19:43:07
回答 1查看 2.2K关注 0票数 2

我正在尝试使用geom_segment绘制一些上下箭头。对于需要向上箭头和向下箭头的数据,我将数据集拆分为两部分。

但在这两个图上,箭头都是朝上的。这种情况如何才能逆转呢?有没有我遗漏的变量?

我使用的数据集如下所示:

代码语言:javascript
复制
GENE   START    END        STRAND
A      3000000  3000312    +
B      3001233  3090123    -

正链需要up-facing箭头,负链需要down-facing

下面是我的代码:

代码语言:javascript
复制
# split the dataset in '+' and '-'
up.arrows <- genes.data[which(genes.data$STRAND=='+'),]
up.arrows.x <- runif(length(up.arrows$START), min = 0, max = length(up.arrows))

down.arrows <- genes.data[which(genes.data$STRAND=='-'),]
down.arrows.x <- runif(length(down.arrows$START), min = 0, max = length(down.arrows))

我使用runif,因为我不关心x-axis的位置。我主要关心的是y-axis

代码语言:javascript
复制
ggplot(up.arrows, aes(up.arrows.x, up.arrows$START)) +
    geom_segment(aes(xend=up.arrows.x, yend=up.arrows$STOP), 
                 arrow = arrow(length = unit(0.1, "cm")),
                 colour = 'green4') + 
    geom_text_repel(size = 3,
                    aes(label = up.arrows$GENE),
                    color = 'black') +
    theme_classic(base_size = 16)

ggplot(down.arrows, aes(down.arrows.x, down.arrows$START)) +
    geom_segment(aes(xend=down.arrows.x, yend=down.arrows$STOP), 
                 arrow = arrow(length = unit(0.1, "cm")),
                 colour = 'green4') + 
    geom_text_repel(size = 3,
                    aes(label = down.arrows$GENE),
                    color = 'black',
                    segment.color = 'black') +
    theme_classic(base_size = 16)

之后,我想合并1ggplot中的点,因此区分updown是很重要的

EN

回答 1

Stack Overflow用户

发布于 2017-01-26 20:32:06

解决方案是一个小技巧。但我花了一些时间去思考。只需颠倒down-facing箭头的启动和停止。

而不是这样:

代码语言:javascript
复制
ggplot(down.arrows, aes(down.arrows.x, down.arrows$START)) +
    geom_segment(aes(xend=down.arrows.x, yend=down.arrows$STOP), 
                 arrow = arrow(length = unit(0.1, "cm")),
                 colour = 'green4') + 
    geom_text_repel(size = 3,
                    aes(label = down.arrows$GENE),
                    color = 'black',
                    segment.color = 'black') +
    theme_classic(base_size = 16)

执行以下操作:

代码语言:javascript
复制
ggplot(down.arrows, aes(down.arrows.x, down.arrows$STOP)) +   # This is all it took
    geom_segment(aes(xend=down.arrows.x, yend=down.arrows$START), # and this
                 arrow = arrow(length = unit(0.1, "cm")),
                 colour = 'green4') + 
    geom_text_repel(size = 3,
                    aes(label = down.arrows$GENE),
                    color = 'black',
                    segment.color = 'black') +
    theme_classic(base_size = 16)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41872725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档