R语言 误差条上限上的字母

ryhaxcpt  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(105)

我试图在如图所示创建的箱线图中添加误差条上限的字母,但它们并不完全是上限的位置。我想把它们精确地放置在每个箱线图的上限,并保持所有其他元素与它们相同。我使用以下代码。

#ANOVA to extract the letters
mod <- aov(gas ~ Genotype, data = data) 
tu <- TukeyHSD(mod)
group <- multcompLetters4(mod, tu)
group_letter <- data.frame(group$`Genotype`$Letters)
# data summerization and added a additional column with letters
df = data %>% group_by(Genotype, Species) %>%
summarize(MaxVal = max(gas), sd=sd(gas),mean=mean(gas),
            median= median(gas),se = sd(gas/sqrt(gas)),
            Q50 = quantile(gas, probs = 0.50),
            Q25 = quantile(gas, probs = 0.25),
            Q75 = quantile(gas, probs = 0.75),
            WhiskUp = (Q75 * (Q75 - Q25)),.groups = "keep")
df$label <- group_letter$group.Genotype.Letters```

bp <- boxplot(gas ~ Genotype, data = data, plot = FALSE)
bp
data %>% 
  transform(Genotype = factor(Genotype, levels = bp$names[order(-bp$stats[4,])])) %>%
  ggplot(aes(x=Genotype, y=Pn, fill=Species)) +
  stat_boxplot(geom = "errorbar") +
  scale_fill_manual(values=c('#e9002d', '#ffaa00', '#00b000')) +
  geom_boxplot(linetype = 3) +
  theme(axis.text.x = element_text(size = 10, colour = "black",vjust = 0.5, angle = 90))+ 
  geom_text(data = df, aes(y = MaxVal-WhiskUp, label = label), fontface="italic", 
            size=3.5, position = position_dodge(width = .75), 
            angle=90, hjust=0.4, vjust=0.3)

这里是数据集,数据集包含属于3个物种和1个变量的42个基因型

Genotype    Species gas
BGB001  sp1 0.214
BGB001  sp1 0.216
BGB001  sp1 0.109
BGB001  sp1 0.097
BGB003  sp1 0.182
BGB003  sp1 0.270
BGB003  sp1 0.181
BGB003  sp1 0.282
BGB008  sp1 0.349
BGB008  sp1 0.206
BGB008  sp1 0.388
BGB008  sp1 0.157
BGB009  sp1 0.170
BGB009  sp1 0.179
BGB009  sp1 0.197
BGB009  sp1 0.164
BGB011  sp1 0.266
BGB011  sp1 0.198
BGB011  sp1 0.410
BGB011  sp1 0.352
BGB027  sp1 0.348
BGB027  sp1 0.344
BGB027  sp1 0.353
BGB027  sp1 0.350
BGB045  sp1 0.311
BGB045  sp1 0.211
BGB045  sp1 0.277
BGB045  sp1 0.340
BGB048  sp2 0.486
BGB048  sp2 0.504
BGB048  sp2 0.187
BGB048  sp2 0.205
BGB055  sp2 0.257
BGB055  sp2 0.273
BGB055  sp2 0.328
BGB055  sp2 0.342
BGB068  sp2 0.238
BGB068  sp2 0.121
BGB068  sp2 0.260
BGB068  sp2 0.133
BGB077  sp2 0.261
BGB077  sp2 0.203
BGB077  sp2 0.275
BGB077  sp2 0.219
BGB083  sp2 0.498
BGB083  sp2 0.485
BGB083  sp2 0.515
BGB083  sp2 0.499
BGB086  sp2 0.471
BGB086  sp2 0.183
BGB086  sp2 0.441
BGB086  sp2 0.134
BGB088  sp3 0.196
BGB088  sp3 0.178
BGB088  sp3 0.174
BGB088  sp3 0.229
BGB089  sp3 0.196
BGB089  sp3 0.448
BGB089  sp3 0.213
BGB089  sp3 0.484
BGB091  sp3 0.143
BGB091  sp3 0.187
BGB091  sp3 0.192
BGB091  sp3 0.155
BGB093  sp3 0.297
BGB093  sp3 0.314
BGB093  sp3 0.497
BGB093  sp3 0.519
BGB094  sp2 0.426
BGB094  sp2 0.335
BGB094  sp2 0.307
BGB094  sp2 0.221
BGB095  sp2 0.128
BGB095  sp2 0.099
BGB095  sp2 0.190
BGB095  sp2 0.186
BGB096  sp2 0.357
BGB096  sp2 0.274
BGB096  sp2 0.350
BGB096  sp2 0.539
BGB097  sp2 0.172
BGB097  sp2 0.174
BGB097  sp2 0.208
BGB097  sp2 0.459
BGB098  sp2 0.379
BGB098  sp2 0.380
BGB098  sp2 0.543
BGB098  sp2 0.557
BGB099  sp2 0.361
BGB099  sp2 0.354
BGB099  sp2 0.296
BGB099  sp2 0.289
BGB100  sp2 0.357
BGB100  sp2 0.529
BGB100  sp2 0.323
BGB100  sp2 0.503
BGB101  sp2 0.231
BGB101  sp2 0.235
BGB101  sp2 0.233
BGB101  sp2 0.236
BGB102  sp2 0.323
BGB102  sp2 0.402
BGB102  sp2 0.389
BGB102  sp2 0.501
BGB103  sp2 0.315
BGB103  sp2 0.314
BGB103  sp2 0.312
BGB103  sp2 0.311
BGB104  sp2 0.423
BGB104  sp2 0.323
BGB104  sp2 0.280
BGB104  sp2 0.160
BGB105  sp2 0.151
BGB105  sp2 0.137
BGB105  sp2 0.170
BGB105  sp2 0.092
BGB106  sp2 0.274
BGB106  sp2 0.279
BGB106  sp2 0.241
BGB106  sp2 0.246
BGB107  sp2 0.137
BGB107  sp2 0.145
BGB107  sp2 0.148
BGB107  sp2 0.133
BGB108  sp2 0.505
BGB108  sp2 0.526
BGB108  sp2 0.351
BGB108  sp2 0.380
BGB109  sp2 0.307
BGB109  sp2 0.283
BGB109  sp2 0.306
BGB109  sp2 0.283
BGB110  sp2 0.148
BGB110  sp2 0.186
BGB110  sp2 0.089
BGB110  sp2 0.318
BGB111  sp2 0.162
BGB111  sp2 0.185
BGB111  sp2 0.125
BGB111  sp2 0.138
BGB113  sp2 0.673
BGB113  sp2 0.780
BGB113  sp2 0.482
BGB113  sp2 0.666
BGB444  sp2 0.384
BGB444  sp2 0.222
BGB444  sp2 0.374
BGB444  sp2 0.190
BGB451  sp2 0.569
BGB451  sp2 0.562
BGB451  sp2 0.641
BGB451  sp2 0.635
BGB453  sp2 0.136
BGB453  sp2 0.159
BGB453  sp2 0.173
BGB453  sp2 0.194
BGB460  sp1 0.127
BGB460  sp1 0.246
BGB460  sp1 0.189
BGB460  sp1 0.314
BGB467  sp2 0.270
BGB467  sp2 0.274
BGB467  sp2 0.273
BGB467  sp2 0.270
BGB472  sp2 0.451
BGB472  sp2 0.634
BGB472  sp2 0.445
BGB472  sp2 0.600
kb5ga3dv

kb5ga3dv1#

我们可以使用bp$stats的顶部晶须,但有一个很大的警告:boxplot(.)stat_boxplot对晶须有不同的理解。参见?geom_boxplot

The lower and upper hinges correspond to the first and third
     quartiles (the 25th and 75th percentiles). This differs slightly
     from the method used by the boxplot() function, and may be
     apparent with small samples. See boxplot.stats() for more
     information on how hinge positions are calculated for boxplot().

因此,其中两个“字母”似乎位于异常值处,而不是晶须上。(我修改了hjust=-0.1。)

df$y <- bp$stats[5,][match(df$Genotype, bp$names)]
head(df)
# # A tibble: 6 × 13
# # Groups:   Genotype, Species [6]
#   Genotype Species MaxVal      sd  mean median      se   Q50   Q25   Q75 WhiskUp label      y
#   <chr>    <chr>    <dbl>   <dbl> <dbl>  <dbl>   <dbl> <dbl> <dbl> <dbl>   <dbl> <chr>  <dbl>
# 1 BGB001   sp1      0.216 0.0649  0.159  0.162 0.0828  0.162 0.106 0.214 0.0233  a      0.216
# 2 BGB003   sp1      0.282 0.0548  0.229  0.226 0.0575  0.226 0.182 0.273 0.0249  ab     0.282
# 3 BGB008   sp1      0.388 0.111   0.275  0.277 0.108   0.277 0.194 0.359 0.0592  abc    0.388
# 4 BGB009   sp1      0.197 0.0144  0.178  0.174 0.0169  0.174 0.168 0.184 0.00275 abcd   0.197
# 5 BGB011   sp1      0.41  0.0934  0.306  0.309 0.0861  0.309 0.249 0.366 0.0431  abcde  0.41 
# 6 BGB027   sp1      0.353 0.00377 0.349  0.349 0.00320 0.349 0.347 0.351 0.00132 abcdef 0.353

data %>% 
  mutate(Genotype = factor(Genotype, levels = bp$names[order(-bp$stats[4,])])) %>%
  ggplot(aes(x=Genotype, y=gas, fill=Species)) +
  stat_boxplot(geom = "errorbar") +
  scale_fill_manual(values=c('#e9002d', '#ffaa00', '#00b000')) +
  geom_boxplot(linetype = 3) +
  theme(axis.text.x = element_text(size = 10, colour = "black",vjust = 0.5, angle = 90))+ 
  geom_text(data = df, aes(y = y, label = label), fontface="italic", 
            size=3.5, position = position_dodge(width = .75), 
            angle=90, hjust=-0.1, vjust=0.3)

相关问题