ggplot2的小提琴图未按x轴数据集中的顺序排列 [英] Violin plot of ggplot2 is not in order as in dataset on x-axis

查看:79
本文介绍了ggplot2的小提琴图未按x轴数据集中的顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了使用下面的代码,但是在x轴上,模型名称与数据集中的顺序不符(即观察到","SVM","Grid_SVM","MARS","Grid_Mars","RF","Grid_RF").代码中需要修改的地方?

I created the using below code, But on the x-axis, Models name are not in order as in the data set (i.e. "Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF"). Where the edit is required in the code?

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))

推荐答案

默认情况下,ggplot2按字母顺序绘制字符向量.要将指定的顺序放置到绘图中,只需使用dplyr并将列创建为 factor()并指定所需的级别.然后ggplot2应该根据需要绘制它.

By default, ggplot2 plots character vectors in alphabetical order. To place a specified order to your plot, simply use dplyr and create the column as a factor() and specify the levels you desire. Then ggplot2 should plot it as you want.

编辑#1 一种方法是与ggplot2命令字符串分开修改 df1 数据帧.您可以按照以下方式进行操作

Edit #1 One way is to modify the df1 data frame separately from your string of ggplot2 commands. You can do this as

df1 <- df1 %>%
  mutate( Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF")) )

然后按照上面的说明调用ggplot2命令字符串.

And then call your string of ggplot2 commands as you have posted above.

编辑#2 (来自下面的评论)

如果您想通过管道传输所有内容并一次完成所有操作,请尝试

If you want to pipe everything through and do everything in one shot, try

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))

最初的建议是覆盖数据.然后在新链中生成图.

The original suggestion was to overwrite the data. Then generate the plot in a new chain.

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))

以上任何一种都可以.

这篇关于ggplot2的小提琴图未按x轴数据集中的顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆