我正在尝试使用geom_segment绘制一些上下箭头。对于需要向上箭头和向下箭头的数据,我将数据集拆分为两部分。
但在这两个图上,箭头都是朝上的。这种情况如何才能逆转呢?有没有我遗漏的变量?
我使用的数据集如下所示:
GENE START END STRAND
A 3000000 3000312 +
B 3001233 3090123 -正链需要up-facing箭头,负链需要down-facing。
下面是我的代码:
# 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。
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中的点,因此区分up和down是很重要的
发布于 2017-01-26 20:32:06
解决方案是一个小技巧。但我花了一些时间去思考。只需颠倒down-facing箭头的启动和停止。
而不是这样:
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)执行以下操作:
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)https://stackoverflow.com/questions/41872725
复制相似问题