在R中使用ggplot将参考线添加到条形图中

纹状体

这是一个最小的示例,显示了我要绘制的图。

数据如下:

plot1 = data.frame(
    Factor1 = as.factor(rep('A', 4)),
    Factor2 = as.factor(rep(c('C', 'D'), 2)),
    Factor3 = as.factor(c( rep('E', 2), rep('F', 2))),
    Y = c(0.225490, 0.121958, 0.218182, 0.269789)
)

plot2 = data.frame(
    Factor1 = as.factor(rep('B', 4)),
    Factor2 = as.factor(rep(c('C', 'D'), 2)),
    Factor3 = as.factor(c( rep('E', 2), rep('F', 2))),
    Y = c(-0.058585, -0.031686, 0.013141, 0.016249)
)

虽然绘图的基本代码如下所示:

require(ggplot2)
require(grid)

p1 <- ggplot(data=plot1, aes(x=Factor2, y=Y, fill=factor(Factor3))) +
    ggtitle('Type: A') +
    coord_cartesian(ylim = c(-0.10, 0.30)) +
    geom_bar(position=position_dodge(.9), width=0.5, stat='identity') +
    scale_x_discrete(name='Regime',
        labels=c('C', 'D')) +
    scale_y_continuous('Activations') +
    scale_fill_brewer(palette='Dark2', name='Background:',
        breaks=c('E','F'),
        labels=c('E','F')) +
    theme(axis.text=element_text(size=11),
        axis.title.x=element_text(size=13, vjust=-0.75),
        axis.title.y=element_text(size=13, vjust=0.75),
        legend.text=element_blank(),
        legend.title=element_blank(),
        legend.position='none',
        plot.title=element_text(hjust=0.5))

p2 <- ggplot(data=plot2, aes(x=Factor2, y=Y, fill=factor(Factor3))) +
    ggtitle('Type: B') +
    coord_cartesian(ylim = c(-0.10, 0.30)) +
    geom_bar(position=position_dodge(.9), width=0.5, stat='identity') +
    scale_x_discrete(name='Regime',
        labels=c('C', 'D')) +
    scale_y_continuous('Activations') +
    scale_fill_brewer(palette='Dark2', name='Background:',
        breaks=c('E','F'),
        labels=c('E','F')) +
    theme(axis.text=element_text(size=11),
        axis.title.x=element_text(size=13, vjust=-0.75),
        axis.title.y=element_blank(),
        legend.text=element_text(size=11),
        legend.title=element_text(size=13),
        plot.title=element_text(hjust=0.5))

pushViewport(viewport(
    layout=grid.layout(1, 2, heights=unit(4, 'null'),
        widths=unit(c(1,1.17), 'null'))))
print(p1, vp=viewport(layout.pos.row=1, layout.pos.col=1))
print(p2, vp=viewport(layout.pos.row=1, layout.pos.col=2))

该图如下所示:

尝试

但是,我需要这样的东西:

正确

黑色粗线为参考值。它们是恒定的,该图表示该“参考情况”。但是,在其他需要制作条形图的图中,该图将发生变化,但参考值应保持不变,以使比较简单明了。我知道我应该使用,geom_segment()但是在进行这项工作的过程中,这些行只是缺少一些限制。

任何帮助/建议吗?谢谢!

阿尔特

我能够使用进行此操作geom_errorbarh例如,对于第二个数字:

p1 + 
  geom_errorbarh(
    aes(xmin = as.numeric(Factor2)-.2,xmax = as.numeric(Factor2)+.2), #+/-.2 for width
    position = position_dodge(0.9), size = 2, height = 0
  )

输出:在此处输入图片说明而且,如果我了解您描述的其他图,则可以在其中指定参考数据,例如data = plot1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用ggplot将误差线添加到R中的聚簇条形图中

将趋势线添加到堆积的条形图中

ggplot将汇总的摘要添加到条形图中

r将线或线段添加到条形图

在ggplot2中将圆形参考线添加到极坐标条形图

r-ggplot2-将差异添加到分组的条形图中

将值添加到R中表的条形图中

将图例添加到条形图(R ggplot)

将标签添加到ggplot堆叠条形图中的整个条形

在条形图中添加参考线

MS Excel-将线添加到堆积条形图中的某些系列

将带有geom_seg的线添加到R中的堆叠条形图中

将线添加到ggplot2中的双条形图

将误差线添加到条形图

将演化线添加到堆叠条形图

根据ggplot中的条件将点添加到堆叠条形图中

将辅助轴添加到堆叠的条形图中

将动画添加到数据排序条形图中的条形图中

将误差线添加到分组的 R 绘图条形图

如何在R中使用ggplot2在条形图中的条形图中添加标签?

Plotly R:无法将段添加到有序条形图中

将 % 添加到条形图 ggplot2

使用多面条形图将文本添加到绘图中

使用highcharter为条形图中的每个条形添加参考线

ggplot:如何将组平均值作为一条线添加到R中的分组条形图?

将样本大小添加到 R 中的 ggplot2 条形图

将条形图上的箭头(或箭头的图像)添加到R中的plotly或ggplot中?

使用ggplot2将文字添加到经过刻面标记的条形图上

如何将非生物变量(温度、水等)作为线条添加到 ggplot2 R 中的条形图中?