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

R / ggplot 2 - dealing uneven group size with Facet_grid and geom histogram / errorbar

我想在我的代码的最后一行强制使用 +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")

给出图 3

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

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

编辑:

有时您可能会发现缺少数据,这会再次导致不平衡条:

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

解决方法是 tidyr 程序包,它允许您包含明确的 NA 值,从而为缺失的柱创建 space。

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