问题描述
我刚刚开始使用 ggplot,并且在创建我需要的方面方面取得了很大进展。但是,当我添加带有重要性注释的数据框时,构面的顺序会发生变化。 可能有太多步骤导致重新排序,因此如果您有任何建议来解决问题和简化代码,请告诉我。
我的代码是:
ggplot(cdata,aes(x= reorder(Speaker_Group,-Overall_Severity),y=Overall_Severity))
geom_bar(aes (fill = Speaker_Group),stat="identity",position=position_dodge())
geom_errorbar(aes(ymin=Overall_Severity-se,ymax=Overall_Severity+se),width=.2,position=position_dodge(.9))
facet_grid(Training_Group_f ~ .,scales = "free_y",space = "free") + scale_fill_manual(values = c("darkgreen","darkred","darkcyan","black"),labels = c("ALS","PD","OA","YA"))
theme(panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank(),panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank())
theme(axis.title.x = element_blank(),axis.text.x = element_text(face = "bold",color = "black",size = 10),axis.title.y = element_text(face = "bold",size = 10))
theme(panel.spacing = unit(1,"lines"))
geom_signif(data = annotation1_df,aes(xmin = start,xmax = end,annotations = label,y_position = y),textsize = 4,vjust = 1.2,manual = TRUE)
如果我删除 geom_signif
调用,则构面的顺序是准确的 (No_Training,Pre_Training,Post_Training
),但是一旦我添加它,它就会重新排序构面 (No_Training,Post_Training,Pre_Training
)。 enter image description here
annotation1_df
由以下给出:
annotation1_df <- data.frame(Training_Group_f = c("No_Training","No_Training","Pre_Training","Post_Training","Post_Training"),start = c("ALS","ALS","ALS"),end = c("PD","YA","YA"),y = c(95,90,80,70,70),label = c("p<.0001","p<.0001","p<.0001")
)
annotation1_df
我还订购了 cdata
中的级别:
cdata$Training_Group_f = factor(cdata$training,levels=c("No_Training","Post_Training"))
cdata$Speaker_Group_f = factor(cdata$Speaker_Group,levels=c("ALS","YA"))
cdata <- ddply(data,c("speaker","training"),summarise,N = length(overallseverity),Overall_Severity = mean(overallseverity),sd = sd(overallseverity),se = sd / sqrt(N)
)
cdata
解决方法
操作。当我查看您的详细问题时,以下注释似乎非常重要:
如果我删除 geom_signif
调用,则刻面的顺序是
准确(No_Training、Pre_Training、Post_Training)但是一旦我添加
它重新排序方面(No_Training、Post_Training、
预训练)。
正如您似乎已经理解的那样,分面顺序是通过 ggplot2
中的两种方式之一确定的:
- 如果您正在对离散值进行分面并且该列已经是一个因子,则分面的顺序将根据该因子的级别顺序进行设置。
- 如果您正在对一个不是因子的离散值进行分面,则该列将转换为因子,并且排序将默认为字母数字顺序
因此,如果您的第一个没有调用 geom_signif()
是准确的,那么问题应该出在该代码中。让我们看看那一行:
geom_signif(
data = annotation1_df,aes(xmin = start,xmax = end,annotations = label,y_position = y),textsize = 4,vjust = 1.2,manual = TRUE)
问题来了。您在引用 data = cdata
之前的绘图代码,并且您正在对 cdata$Training_Group_f
进行分面。对 geom_signif()
的调用引用了 data = annotation1_df
,它也有 annotation_df$Training_Group_f
。绘制两者都会创建一个 ggplot
对象,该对象组合数据集 cdata
(其中 Training_Group_f
方面的级别设置正确)和 annotation1_df
,其中级别显然未设置.
答案应该是首先确保Training_Group_f
的列设置为两个 数据集cdata
和annotation1_df
中具有相同水平的因子。您需要执行这两行,然后您的构面顺序应该是正确的:
cdata$Training_Group_f = factor(cdata$training,levels=c("No_Training","Pre_Training","Post_Training"))
annotation1_df$Training_Group_f = factor(annotation1_df$training,"Post_Training"))
注意:由于 y = 0 位于图的左下角,您实际上可能会看到级别被颠倒了……我现在不记得这是否是刻面的定位方式,但如果是这样,只需颠倒两者的级别顺序即可。