我想知道我的代码有什么问题?
我的代码就像:
a=aov(tem.range~Butterfly.colonies, data=my.data)
summary(a)
tHSD <- TukeyHSD(a, ordered = FALSE, conf.level = 0.95)
tHSD
generate_label_df <- function(HSD, flev){
# Extract labels and factor levels from Tukey post-hoc
Tukey.levels <- HSD[[flev]][,4]
Tukey.labels <- multcompLetters(Tukey.levels)['Letters']
plot.labels <- names(Tukey.labels[['Letters']])
boxplot.df <- ddply(my.data, flev, function (x) max(fivenum(x$y)) + 0.2)
plot.levels <- data.frame(plot.labels, labels = Tukey.labels[['Letters']], stringsAsFactors = FALSE)
# Merge it with the labels
labels.df <- merge(plot.levels, boxplot.df, by.x = 'plot.labels', by.y = flev, sort = FALSE)
return(labels.df)
}
ggplot(my.data, aes(x=Butterfly.colonies, y=tem.range))+
geom_boxplot(width = 0.5) +
scale_y_continuous(breaks = seq(0, 20, by = 2))+
theme(plot.margin=unit(c(1,3,1,3),"cm"))+
labs(y="tem.range(Celsius")+
labs(x="Colony")+
geom_text(data = generate_label_df(tHSD, 'Butterfly.colonies'),
aes(x = plot.labels, y = V1, label = labels))
然而,最终出现了错误
因此,我无法将标签放在我的图表上
我希望它会像:
答案 0 :(得分:0)
我的代码:
a=aov(tem.range~Butterfly.colonies,data=my.data)
tHSD <- TukeyHSD(a, ordered = FALSE, conf.level = 0.95)
generate_label_df <- function(HSD, flev){
Tukey.levels <- HSD[[flev]][,4]
Tukey.labels <- multcompLetters(Tukey.levels)['Letters']
plot.labels <- names(Tukey.labels[['Letters']])
boxplot.df <- ddply(my.data, flev, function (x) max(fivenum(x$y)) + 0.2)
plot.levels <- data.frame(plot.labels, labels = Tukey.labels[['Letters']],stringsAsFactors = FALSE)
labels.df <- merge(plot.levels, boxplot.df, by.x = 'plot.labels', by.y = flev, sort = FALSE)
return(labels.df)}
ggplot(my.data, aes(x=Butterfly.colonies, y=tem.range))+geom_boxplot(width = 0.5) + scale_y_continuous(breaks = seq(0, 20, by = 2))+ theme(plot.margin=unit(c(1,3,1,3),"cm"))+labs(y="tem.range(Celsius")+labs(x="Colony")+geom_text(data = generate_label_df(tHSD, 'Butterfly.colonies'), aes(x = plot.labels, y = V1, label = labels))