R / ggplot 2 - 使用Facet_grid和geom histogram / errorbar处理不均匀的组大小

时间:2017-05-10 20:11:25

标签: r ggplot2 histogram errorbar facet-grid

我想在我的代码的最后一行强制使用+facet_grid(.~sample,scales = "free_x")进行分面,但结果看起来非常不合理(见图2)(以我的拙见)。我想知道是否有办法强制每个geom_histogram条的特定大小,以确定组之间的条形是否相同,无论组是否平衡。

谢谢, 维维

示例数据:

samplenote  prod    N   mean    sd  se  sampleprod  sample
Sample A    PRODUCT A   3   0.562103162 0.120039901 0.069305069 Sample A PRODUCT A  Sample A
Sample A    PRODUCT B   3   0.516322045 0.039250354 0.022661203 Sample A PRODUCT B  Sample A
Sample B    PRODUCT A   3   0.504789098 0.055005623 0.031757511 Sample B PRODUCT A  Sample B
Sample B    PRODUCT B   3   0.564334594 0.035685751 0.020603178 Sample B PRODUCT B  Sample B
Sample C    PRODUCT A   3   0.337747481 0.042670562 0.024635861 Sample C PRODUCT A  Sample C
Sample C    PRODUCT B   3   0.470207809 0.012102641 0.006987463 Sample C PRODUCT B  Sample C
Sample C group1 PRODUCT A   3   0.666033925 0   0   Sample C group1 PRODUCT A   Sample C
Sample C group1 PRODUCT B   3   0.775242276 0.017019353 0.009826128 Sample C group1 PRODUCT B   Sample C
Sample C group2 PRODUCT A   3   0.53594287  0.062336653 0.035990084 Sample C group2 PRODUCT A   Sample C
Sample C group2 PRODUCT B   3   0.4705616   0.009122911 0.005267115 Sample C group2 PRODUCT B   Sample C

示例图1:

ggplot(data=test.df,aes(x=samplenote,y=mean,fill=prod))+
geom_bar(stat="identity",col="black",size = 0.4,position='dodge')+
scale_fill_manual(values=c("#B50000","#0039e6"))+
geom_errorbar(data=test.df,aes(x=samplenote,ymax=mean+sd,ymin=mean,width=.2),position=position_dodge(.9),colour="black",size = 0.4)+
theme_classic()+
theme(axis.text=element_text(colour="black"))+
theme(axis.ticks=element_line(colour="black"))+
    coord_cartesian(ylim=c(0,1.13),expand = TRUE)+
scale_y_continuous(expand=c(0,0),breaks=c(0,0.25,0.5,0.75,1))+
ylab("g/g prod")+
xlab("")+
theme(legend.title=element_blank())+
theme(axis.line=element_line(size=0.4))

graph1 graph2

修改

Brian给出的解决方案:

ggplot(data=test.df,aes(x=samplenote,y=mean,fill=prod))+
geom_bar(stat="identity",col="black",size = 0.4,position='dodge')+
scale_fill_manual(values=c("#B50000","#0039e6"))+
geom_errorbar(data=test.df,aes(x=samplenote,ymax=mean+sd,ymin=mean,width=.2),position=position_dodge(.9),colour="black",size = 0.4)+
theme_classic()+
theme(axis.text=element_text(colour="black"))+
theme(axis.ticks=element_line(colour="black"))+
    coord_cartesian(ylim=c(0,1.13),expand = TRUE)+
scale_y_continuous(expand=c(0,0),breaks=c(0,0.25,0.5,0.75,1))+
ylab("g/g prod")+
xlab("")+
theme(legend.title=element_blank())+
theme(axis.line=element_line(size=0.4))+facet_grid(.~sample,scales = "free_x",space="free_x")

给出graph3 graph3

1 个答案:

答案 0 :(得分:3)

您需要使用+ facet_grid(~ sample, scales = "free_x", space = "free_x")space参数调整构面的大小,使条形宽度保持一致(或更准确,以便X轴上的刻度之间的间距为)。

require(dplyr)
data_frame(x = c("a", "a", "b", "b", "c", "c"),
           y = runif(length(x)),
           sample = rep(c("A", "B"), 3),
           grouping = c(1, 1, 1, 1, 2, 2)) %>% 
  ggplot(aes(x, y, fill = sample)) + geom_bar(stat = "identity", position = "dodge") + 
  facet_grid(~grouping, space = "free_x", scales = "free_x")

enter image description here

编辑:

有时候,您可能会发现自己错过了数据并再次导致高低杠:

data_frame(x = c("a", "a", "b", "b", "c", "c"),
           y = runif(length(x)),
           sample = rep(c("A", "B"), 3),
           grouping = c(1, 1, 1, 2, 2, 2)) %>% 
  ggplot(aes(x, y, fill = sample)) + geom_bar(stat = "identity", position = "dodge") + 
  facet_grid(~grouping, space = "free_x", scales = "free_x")

enter image description here

对此的修正是tidyr包,它允许您包含明确的NA值,这会为缺少的条形成一个空间。

data_frame(x = c("a", "a", "b", "b", "c", "c"),
           y = runif(length(x)),
           sample = rep(c("A", "B"), 3),
           grouping = c(1, 1, 1, 2, 2, 2)) %>% 
  group_by(grouping) %>% 
  tidyr::complete(crossing(sample, x)) %>% 
  ggplot(aes(x, y, fill = sample)) + geom_bar(stat = "identity", position = "dodge") + 
  facet_grid(~grouping, space = "free_x", scales = "free_x")

enter image description here