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

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

问题描述

我有一个数据集,可以在不同的日子为不同的群体进行测量。



我想要在不同日期不同的组和棒组之间的间隔根据测量日的不同而有所不同。

我在 geom_bar 同意闪烁在 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 =合并(天,群);


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),color =black)+
geom_point(position = position_dodge(width = 0.5),aes(y = mid,color = group )));
dev.off();

在图中,错误出现时显示一个固定偏移量(对不起,不允许绘图新手,即使ggplot2是主题)。



geom_bar 中调整了binwidth时,偏移并不固定,并且每天都在变化。



请注意, geom_errorbar geom_point 串联闪避。
如何获得 geom_bar 与其他两个人同意?



p>

解决方案

对齐问题部分是由于您的酒吧不能代表您想要的数据。以下是正确的:

  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),color =black)+
geom_point(position = position_dodge(width = 0.9),aes(y = mid,color = group))


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.

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

Here is a simple piece of code:

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();

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).

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

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

Any help appreciated.

解决方案

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天全站免登陆