ggplot为每个组绘制特定的边界?

问题描述 投票:2回答:1

嗨假设我有这样的数据帧。

id     gene    value      upper    lower
AE5    ATM 4.046142  0.5440875 5.941381
AE5    ATR 3.463361  1.5046214 4.867110
AE5  BRCA1 4.228049 -0.7397759 5.791135
AE5  CDK12 4.488001  1.6029831 6.106177
AE5 CDKN1A 4.837943  2.1936042 9.880194
AE6    ATM 3.629939  0.5440875 5.941381
AE6    ATR 3.121015  1.5046214 4.867110
AE6  BRCA1 4.368070 -0.7397759 5.791135
AE6  CDK12 4.759688  1.6029831 6.106177
AE6 CDKN1A 5.757290  2.1936042 9.880194

我可以用ggplot绘制这个

ggplot(final , aes(y=gene, x=value, col=id)) +
  geom_point(size=5)

它让我在这里得到这个情节。 enter image description here

然而,我想要的是根据数据帧的上下列为每个组设置边界线。因此,例如ATM将具有在.54和5.9上交叉的小垂直线。通过这种方式,我可以更好地想象每个样本落地的位置。提前致谢!

r ggplot2
1个回答
5
投票

你可以使用geom_errorbarh包中的ggstance。要获得单个范围线而不管id(因为id的两个级别的范围相同),将颜色美学移动到geom_point,这样它将仅适用于点而不是误差条。我们还在geom_errorbarh中设置数据以仅选择一个id,以避免将相同的误差条多次绘制在彼此之上。

library(ggplot2)
library(ggstance)

ggplot(final , aes(y=gene, x=value)) +
  geom_errorbarh(data=final[final$id=="AE5",], aes(xmin=lower, xmax=upper),
                 width=0.2, colour="grey50") +
  geom_point(size=5, aes(col=id)) +
  theme_bw()

enter image description here

为了获得垂直线,您可以使用geom_point作为点标记对"|"进行两次调用(尽管我认为使用水平线来引导眼睛更容易阅读图形)。

ggplot(final , aes(y=gene, x=value)) +
  geom_point(aes(x=upper), shape="|", size=5) +
  geom_point(aes(x=lower), shape="|", size=5) +
  geom_point(size=5, aes(col=id)) +
  theme_bw()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.