【问题标题】:Violin plot of ggplot2 is not in order as in dataset on x-axisggplot2 的小提琴图不像 x 轴上的数据集那样有序
【发布时间】:2020-08-15 21:55:01
【问题描述】:

我使用以下代码创建了,但在 x 轴上,模型名称与数据集中的顺序不同(即“已观察”、“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))

【问题讨论】:

    标签: r ggplot2 axis-labels violin-plot


    【解决方案1】:

    默认情况下,ggplot2 按字母顺序绘制字符向量。要为您的绘图下一个指定的订单,只需使用 dplyr 并将列创建为 factor() 并指定您想要的级别。然后 ggplot2 应该根据需要绘制它。

    编辑#1 一种方法是将 df1 数据框与您的 ggplot2 命令字符串分开修改。你可以这样做

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

    然后调用您在上面发布的 ggplot2 命令字符串。

    编辑#2(来自下面的 cmets)

    如果您想一次性完成所有操作,请尝试

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

    以上任何一种都可以。

    【讨论】:

    • ggplot2 还有order 美学。我从未使用过它,但您可能也想尝试一下。
    • 感谢第一时间回复,如何执行,请描述或提供线索
    • 我得到了使用时的最新建议:错误:应使用aes() or aes_(). Run rlang::last_error()` 创建映射以查看错误发生的位置。
    • 前三行:df1 % mutate(Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars" , "RF", "Grid_RF"))) %>% ggplot(df1, aes(x = Model, y = Pb))
    • 哇,非常感谢,我使用了原来的建议,然后调用了 ggplot,这个工作完美。但是,我会保留第二个建议,非常感谢。
    猜你喜欢
    • 2021-06-15
    • 2018-12-10
    • 2017-08-11
    • 2019-01-21
    • 2018-05-19
    • 1970-01-01
    • 2018-01-15
    • 2016-06-13
    • 2017-01-04
    相关资源
    最近更新 更多