如何使 geom_bar 中的闪避与 geom_errorbar、geom_point 中的闪避一致 [英] How to make dodge in geom_bar agree with dodge in geom_errorbar, geom_point

查看:23
本文介绍了如何使 geom_bar 中的闪避与 geom_errorbar、geom_point 中的闪避一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中在不同的日子对不同的群体进行了测量.

I have a dataset where measurements are made for different groups at different days.

我想要并排的条形表示不同组在不同天的测量值,条形组根据测量日期间隔开,误差条覆盖在它们上.

I want to have side by side bars representing the measurements at the different days for the different groups with the groups of bars spaced according to day of measurement with errorbars overlaid to them.

我无法让 geom_bar 中的闪避与 geom_errorbar 上的闪避一致.

I'm having trouble with making the dodging in geom_bar agree with the dodge on geom_errorbar.

这是一段简单的代码:

days          = data.frame(day=c(0,1,8,15));
groups        = data.frame(group=c("A","B","C","D", "E"), means=seq(0,1,length=5));


my_data       = merge(days, groups);


my_data$mid   = exp(my_data$means+rnorm(nrow(my_data), sd=0.25));
my_data$sigma = 0.1;


png(file="bar_and_errors_example.png", height=900, width=1200);
plot(ggplot(my_data, aes(x=day, weight=mid, ymin=mid-sigma, ymax=mid+sigma, fill=group)) +
     geom_bar      (position=position_dodge(width=0.5))                                   +
     geom_errorbar (position=position_dodge(width=0.5), colour="black")                   +
     geom_point    (position=position_dodge(width=0.5), aes(y=mid, colour=group)));
dev.off();

在图中,errorsegments 出现时与其条形有固定的偏移(抱歉,即使 ggplot2 是主题,也不允许新手绘制图).

In the plot, the errorsegments appears with a fixed offset from its bar (sorry, no plots allowed for newbies even if ggplot2 is the subject).

geom_bar中调整binwidth时,偏移量不是固定的,每天都在变化.

When binwidth is adjusted in geom_bar, the offset is not fixed and changes from day to day.

注意,geom_errorbargeom_point 会同时闪避.我如何让 geom_bar 同意其他两个?

Notice, that geom_errorbar and geom_point dodge in tandem. How do I get geom_bar to agree with the other two?

感谢任何帮助.

推荐答案

对齐问题部分是由于您的条形不代表您想要的数据.以下排列正确:

The alignment problems are due, in part, to your bars not representing the data you intend. The following lines up correctly:

ggplot(my_data, aes(x=day, weight=mid, ymin=mid-sigma, ymax=mid+sigma, fill=group)) +
     geom_bar      (position=position_dodge(), aes(y=mid), stat="identity") +
     geom_errorbar (position=position_dodge(width=0.9), colour="black") +
     geom_point    (position=position_dodge(width=0.9), aes(y=mid, colour=group))

这篇关于如何使 geom_bar 中的闪避与 geom_errorbar、geom_point 中的闪避一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆