【问题标题】:Bar graph with error bar in ggplot2ggplot2中带有误差线的条形图
【发布时间】:2016-07-15 15:23:31
【问题描述】:

我正在尝试绘制带有误差线和重要性星的条形图。一切都很好,除了意义星落在看起来很奇怪的错误栏上。是否可以单独调整重要性星。谢谢。

R 代码

dat <- read.delim("read_count1", header=T)
dat2 <- dat
dat2$condition <- factor(dat2$condition)
dat2$star <- ""
dat2$star[dat$p == .001]  <- "*"
dat2$star[dat$p == 0.0001]  <- "**"

ggplot(dat2, aes(x=group, y=value, fill=condition)) + 
geom_bar(colour="black", stat="identity", position=position_dodge(),
         size=.3,width=.5) +                         # Thinner lines
xlab("Condition") + ylab("Viral transcript count") + # Set axis labels
theme_bw()  +
theme(legend.position=c(0.85, .9), legend.direction = "vertical", 
      legend.key.size = unit(4, 'lines'), legend.title=element_blank()) +
theme(axis.text = element_text(size=8), axis.title = element_text(size=8,face="bold")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_errorbar(aes(ymin=value-sd, ymax=value+sd),
              width = 0.2,                           # Width of the error bars
              position = position_dodge(-.5)) + 
geom_text(aes(label=star), colour="red", vjust=0, size=5, position=position_dodge(.5)) +
scale_y_continuous(expand=c(0,0), limits = c(0, 13536400))+
scale_x_discrete("Viral gene")

我的数据

condition   value   sd  group   p
1h  968738.666666667    131475.799527264    Capsid protein  
1h  340500.666666667    48855.1227235521    Nucleic acid binding    protein 
1h  604250.666666667    100141.322860912    Triple gene block p1    
1h  293140.333333333    48797.8510831505    Triple gene block p2    
1h  91655.3333333333    20761.7967992497    Triple gene block p3    
1h  3615029.33333333    635586.634090376    Replication polyprotein 
4h  2787724 245341.698797819    Capsid protein  0.001
4h  971762.666666667    99832.8035483995    Nucleic acid binding    protein 0.001
4h  2017158.33333333    107557.964732201    Triple gene block p1    0.0001
4h  949650  50649.2672306323    Triple gene block p2    0.0001
4h  286073.666666667    8140.5804051882 Triple gene block p3    0.0001
4h  11525318.6666667    1425912.09504525    Replication polyprotein 0.001

【问题讨论】:

  • 您可以 dput() 您的数据以使其更易于阅读吗?
  • 我的第一个想法是,如果您增加 y 轴上限,星号可能能够根据需要将自身置于误差线上方。 (您可能还想将图例从右上角移到左上角)
  • 您正在将文本的 y 位置映射到 value。如果您将其映射到 value + sd,您将从误差线的上限开始所有星号,而不是从误差线的顶部开始。
  • 我不知道怎么做 dput()。增加 y 轴也会增加其他点的距离。我只想修改“多蛋白”。谢谢

标签: r ggplot2


【解决方案1】:

通过将 y 位置映射为value + sdgeom_text 内,将文本层放置在误差线的顶部,而不是误差线的顶部。

geom_text(aes(y = value + sd, label = star),...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多