我正在運行一些統計資料,并且需要在每個條形圖上用平均分隔字母顯示我的結果。但是,我在將我的平均分隔指示符字母分隔在每個條上時遇到問題。
這是我的資料。運行此代碼應生成一個資料框:
data <- data.frame(group = c(1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
4,
4,
4,
4,
4,
4,
4,
4,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5,
5),
habitat = c('woodlands',
'woodlands',
'woodlands',
'woodlands',
'beach',
'beach',
'beach',
'beach',
'prairie',
'prairie',
'prairie',
'prairie',
'woodlands',
'woodlands',
'woodlands',
'woodlands',
'beach',
'beach',
'beach',
'beach',
'prairie',
'prairie',
'prairie',
'prairie',
'woodlands',
'woodlands',
'woodlands',
'woodlands',
'prairie',
'prairie',
'prairie',
'prairie',
'beach',
'beach',
'beach',
'beach',
'prairie',
'prairie',
'prairie',
'prairie',
'beach',
'beach',
'beach',
'beach',
'beach',
'beach',
'beach',
'beach',
'prairie',
'prairie',
'prairie',
'prairie',
'woodlands',
'woodlands',
'woodlands',
'woodlands'),
species = c('skunk',
'fox',
'raccoon',
'gopher',
'raccoon',
'fox',
'skunk',
'gopher',
'skunk',
'fox',
'gopher',
'raccoon',
'gopher',
'skunk',
'raccoon',
'fox',
'raccoon',
'skunk',
'fox',
'gopher',
'gopher',
'fox',
'skunk',
'raccoon',
'raccoon',
'skunk',
'fox',
'gopher',
'gopher',
'fox',
'skunk',
'raccoon',
'gopher',
'skunk',
'raccoon',
'fox',
'skunk',
'fox',
'raccoon',
'gopher',
'skunk',
'gopher',
'raccoon',
'fox',
'skunk',
'gopher',
'fox',
'raccoon',
'skunk',
'gopher',
'raccoon',
'fox',
'fox',
'raccoon',
'gopher',
'skunk'),
response = c(321684201,
321684221,
321684211,
321684216,
321684201,
321684221,
321684216,
321684216,
321684211,
321684221,
321684226,
321684206,
321684216,
321684216,
321684216,
321684216,
321684211,
321684211,
321684226,
321684216,
321684221,
321684216,
321684216,
321684216,
321684211,
321684216,
321684226,
321684216,
321684216,
321684221,
321684201,
321684206,
321684216,
321684221,
321684211,
321684221,
321684206,
321684226,
321684226,
321684226,
321684211,
321684216,
321684206,
321684206,
321684211,
321684221,
321684216,
321684206,
321684171,
321684186,
321684181,
321684161,
321684226,
321684216,
321684221,
321684201))
這是我嘗試過的:
# Open packages
library(lme4)
library(ggplot2)
library(emmeans)
library(car)
# Convert columns to factors
data$group <- as.factor(data$group)
data$habitat <- as.factor(data$habitat)
data$species <- as.factor(data$species)
# Generate model and investigate significance
mod <- lmer(response ~ habitat * species (1|group) (1|group:habitat), data = data)
Anova(mod, type = ("II"))
# Interaction term isn't significant at the 0.05 level, but let's assume it is for this question.
# Separate means using the emmeans package
mod_means_cotr <- emmeans(mod, pairwise ~ 'habitat:species',
adjust = 'tukey')
mod_means <- multcomp::cld(object = mod_means_cotr$emmeans,
LETTERS = "letters")
mod_means$hsd <- c('bb',
'abbb',
'abbb',
'abbb',
'abbb',
'abbb',
'abbb',
'aa',
'abbb',
'abbb',
'abbb',
'abbb') # This is done to illustrate situations with long mean separator sequences
mod_means
# Generate the graph
ggplot(mod_means, aes(fill = species, x = habitat, y = emmean))
geom_bar(stat="identity", width = 0.6, position = "dodge", col = "black")
geom_errorbar(aes(ymin = emmean, ymax = emmean SE), width = 0.3, position = position_dodge(0.6))
guides(fill = guide_legend("Species"))
xlab("Habitat") ylab("Response")
theme(plot.title=element_text(hjust=0.5, size = 25),
axis.text = element_text(size = 20),
axis.title = element_text(size = 20))
ggtitle("Graph Title")
geom_text(aes(label=hsd, y=emmean SE), vjust = -0.9, size = 5)
ylim(0, 350000000)
從圖中可以看出,平均間隔都是重疊的,無法區分。此外,這些條不被任何空格分隔。我想在條之間添加空格并將長平均分隔符包裹在每個相應的條上;我假設這個stringr包裹會派上用場,但我不確定。我該怎么做這些事情?
uj5u.com熱心網友回復:
您的示例中的誤差條沒有顯示,因為 SE 與條本身相比是無窮小的,因此出于示例的目的,我將它們乘以 1,000,000。本質上,問題似乎是您沒有species在文本或錯誤欄圖層中用作分組變數:
# Generate the graph
ggplot(mod_means, aes(fill = species, x = habitat, y = emmean))
geom_bar(stat="identity", width = 0.6, position = position_dodge(width = 0.8),
col = "black")
geom_errorbar(aes(ymin = emmean - SE * 1e6, ymax = emmean SE * 1e6,
group = species), width = 0.3,
position = position_dodge(0.8))
guides(fill = guide_legend("Species"))
xlab("Habitat") ylab("Response")
theme(plot.title=element_text(hjust = 0.5, size = 25),
axis.text = element_text(size = 20),
axis.title = element_text(size = 20))
ggtitle("Graph Title")
geom_text(aes(label = hsd, y = emmean SE, group = species), vjust = -0.9,
size = 5, position = position_dodge(width = 0.8))
ylim(0, 350000000)

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527523.html
標籤:rggplot2
