默认情况下,ggplot2 按字母顺序绘制字符向量。要对绘图放置指定的顺序,只需使用 dplyr 并将列创建为factor()
并指定您想要的级别。然后 ggplot2 应该按照你想要的方式绘制它。
Edit #1一种方法是修改df1
数据框与 ggplot2 命令字符串分开。你可以这样做
df1 <- df1 %>%
mutate( Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF")) )
然后调用您上面发布的 ggplot2 命令字符串。
Edit #2(来自下面的评论)
如果您想通过管道传输所有内容并一次性完成所有操作,请尝试
df1 %>%
mutate( Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF")) ) %>%
ggplot( aes(x = Model, y = Pb), cex.lab = 18, cex.axis=18) +
geom_violin(trim=FALSE, fill = "palegreen") +
geom_boxplot(width = .12, fill = "orange", outlier.color = "orange", outlier.size = 2) +
labs(title = "Pb Adsorption distribution by Models over testing phase") + xlab("Models") + ylab("Pb Adsorption distribution")+ theme_classic() +
theme(
plot.title = element_text(size = 12, colour = "black", face = "bold"), #for Main Title
axis.title.x = element_text(size = 12, colour = "black", face = "bold"), #for axix title
axis.title.y = element_text(size = 12, colour = "black", face = "bold"),
axis.text.x = element_text(face="bold", color="darkblue", size=12, angle=0), #for axis tick
axis.text.y = element_text(face="bold", color="black", size=12, angle=0))
最初的建议是覆盖数据。然后在新的链中生成绘图。
df1 <- df1 %>%
mutate( Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF")) )
ggplot( df1, aes(x = Model, y = Pb), cex.lab = 18, cex.axis=18) +
geom_violin(trim=FALSE, fill = "palegreen") +
geom_boxplot(width = .12, fill = "orange", outlier.color = "orange", outlier.size = 2) +
labs(title = "Pb Adsorption distribution by Models over testing phase") + xlab("Models") + ylab("Pb Adsorption distribution")+ theme_classic() +
theme(
plot.title = element_text(size = 12, colour = "black", face = "bold"), #for Main Title
axis.title.x = element_text(size = 12, colour = "black", face = "bold"), #for axix title
axis.title.y = element_text(size = 12, colour = "black", face = "bold"),
axis.text.x = element_text(face="bold", color="darkblue", size=12, angle=0), #for axis tick
axis.text.y = element_text(face="bold", color="black", size=12, angle=0))
以上任何一个都可以。