如何使用facet wrap

时间:2015-08-03 12:42:29

标签: r

当我在条形图上方制作带有来自anova的重要字母的条形图时,我使用以下代码:

anova_NDW_geel<-aov(nodule_dry_weight~treatment,inoculatieproef_geel_variety2)
HSD_NDW_geel <- HSD.test(anova_NDW_geel,"treatment",alpha=0.05,group=TRUE)$groups
HSD_NDW_means_geel <- HSD.test(anova_NDW_geel,"treatment",alpha=0.05,group=TRUE)$means
HSD_NDW_means_geel <- HSD_NDW_means_geel[order(-HSD_NDW_means_geel$nodule_dry_weight),] 

p_HSD_NDW_geel <- ggplot(aes(x=treatment, y=NDW_mean_geel, width=0.6), data=inoculatieproef_mean_geel)+
  geom_bar(stat="identity", data=HSD_NDW_geel, aes(x=trt, y=means), fill="gray40")+
  geom_text(data=HSD_NDW_geel, aes(x=trt, y=means, label=M), size=5, vjust=-1, hjust=1)+
  ggtitle("Zand")+
  ylab("Droog gewicht wortelknolletjes (g)")+
  xlab("Behandeling")+
  geom_errorbar(aes(ymin=NDW_mean_geel-NDW_sd_geel,ymax=NDW_mean_geel+NDW_sd_geel),
                position=position_dodge(width=0.5),width=0.1,size=0.3)+
  theme_bw() + 
  theme(axis.line =  element_line(colour="black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())+
  scale_y_continuous(expand = c(0, 0))+
  theme(axis.text.x = element_text(angle = 0, hjust = 1, vjust = 0.5))+
  theme(text = element_text(size=12))

会生成以下图表:http://i.stack.imgur.com/bZidZ.png

这可能不是最好的方法,当我想用​​小平面包裹将字母添加到条形图时。

以下是我想要使用重要字母进行构面包装的数据示例:

structure(list(treatment = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 1L, 2L, 3L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2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2014", 
    "2015"), class = "factor"), nodule_dry_weight = c(0, 0.0467, 
    0.0328, 0.0885, 0.0081, 0.1086, 0.0788, 0.0267, 0, 0.0128, 0.0143, 
    0.0333, 0.006, 0.098, 0.0286, 0.011, 0, 0.0627, 0.0769, 0.0784, 
    0.023, 0.1504, 0.1026, 0.0254, 0, 0.0597, 0.0158, 0.0354, 0.0226, 
    0.3261, 0.0436, 0, 0, 0.0203, 0.0469, 0.0904, 0.1593, 0.0836, 
    0.056, 0.0037, 0, 0.0534, 0.0901, 0.0435, 0.0248, 0.0435, 0.0279, 
    0.0029, 0, 0.0545, 0.038, 0.0991, 0.0099, 0.1453, 0.1096, 0.0272, 
    0, 0.0319, 0.0624, 0.0508, 0.0415, 0.11, 0.0079, 0, 0, 0.1257, 
    0.1242, 0.2899, 0.024, 0.2175, 0.2979, 0.0396, 0, 0.1583, 0.2935, 
    0.2541, 0.1027, 0.4196, 0.2059, 0.0396, 0, 0.0891, 0.167, 0.0907, 
    0.2153, 0.3063, 0.2921, 0.0528, 0, 0.0928, 0.2109, 0.1514, 0.0821, 
    0.3607, 0.0996, 0.0069, 0, 0.0685, 0.3109, 0.1862, 0.0393, 0.286, 
    0.3418, 0.0459, 0, 0.0765, 0.3486, 0.3988, 0.1155, 0.6341, 0.3653, 
    0.039, 0, 0.0766, 0.3112, 0.1988, 0.05, 0.2856, 0.34, 0.0862, 
    0, 0.2621, 0.1146, 0.393, 0.1644, 0.3415, 0.1343, 0.019, 0, 0.0976, 
    0.1853, 0.0691, 0.0248, 0.1764, 0.1244, 0.1525, 0, 0.1529, 0.1069, 
    0.2833, 0.0204, 0.2966, 0.2371, 0.1464, 0, 0.0691, 0.2094, 0.1633, 
    0.0264, 0.1344, 0.0694, 0.1175, 0, 0.1783, 0.1434, 0.2136, 0.0873, 
    0.19, 0.1683, 0.1927, 0, 0.0571, 0.0599, 0.1061, 0.0244, 0.1256, 
    0.0894, 0.0123, 0, 0.1696, 0.1046, 0.2164, 0.0939, 0.1552, 0.2942, 
    0.1652, 0, 0.0844, 0.102, 0.0227, 0.025, 0.0654, 0.1234, 0.0702, 
    0, 0.0979, 0.1246, 0.0958, 0.0867, 0.1104, 0.1969, 0.227, 0, 
    0.3704, 0.4727, 0.2527, 0.2078, 0.3377, 0.308, 0.1293, 0, 0.2417, 
    0.3744, 0.2916, 0.1773, 0.433, 0.2446, 0.1382, 0, 0.4718, 0.4271, 
    0.4882, 0.1799, 0.4178, 0.518, 0.3915, 0, 0.3421, 0.3804, 0.2112, 
    0.4292, 0.3829, 0.1315, 0.2719, 0, 0.3197, 0.6867, 0.414, 0.3112, 
    0.2914, 0.4994, 0.369, 0.0256, 0.1494, 0.5577, 0.2538, 0.3854, 
    0.4151, 0.544, 0.4009, 0, 0.5208, 0.2962, 0.4175, 0.2689, 0.3374, 
    0.5075, 0.3601, 0, 0.704, 0.4631, 0.4573, 0.154, 0.5087, 0.4319, 
    0.4155)), .Names = c("treatment", "block", "position", "variety", 
    "location", "year", "nodule_dry_weight"), row.names = c(NA, -256L
    ), class = "data.frame")

我使用facet wrap为我的图形使用以下代码:

inoculatieproef <- inoculatieproef %>% 
  group_by(treatment, location, variety, year) %>% 
  mutate(NDW_mean = mean(nodule_dry_weight),
         NDW_sd = sd(nodule_dry_weight)) 

ggplot(data=inoculatieproef,aes(x=treatment, y=NDW_mean))+
  facet_wrap(~location*variety*year,ncol=2)+
  geom_bar(position="dodge", stat="identity")+
  geom_errorbar(aes(ymin = NDW_mean - NDW_sd, 
                    ymax = NDW_mean + NDW_sd),
                width=0.1,size=0.3, 
                color = "darkgrey")+
  theme_bw() + 
  theme(axis.line =  element_line(colour="black"),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())

如何在每个条形图上添加刻面包装图中的重要字母(anova)?

1 个答案:

答案 0 :(得分:0)

不知道测试是否适合您的数据分布,但是您可以从此开始:

library(tidyverse)
stat_pvalue <- dd %>%
  group_by(location, variety, year) %>% 
  rstatix::t_test(nodule_dry_weight~treatment)  %>% 
  filter(p < 0.05) %>% 
  group_by(location, variety, year) %>% 
  rstatix::add_significance("p") %>% 
  rstatix::add_y_position() %>% 
  mutate(y.position = seq(min(y.position), max(y.position),length.out = n())*1.1) %>% 
  ungroup()

ggplot(data=dd,aes(x=treatment, y=nodule_dry_weight))+
  geom_boxplot() + 
  facet_wrap(~location + variety + year,ncol=2, scales = "free_y") +
  ggpubr::stat_pvalue_manual(stat_pvalue, label = "p")

enter image description here