You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用facet.by分面绘图后如何调整分面图形的排列顺序

ggscatter分面排序修改方法

分面默认按照字符的字母/拼音顺序排列,要自定义排序只需要将分组字段Distance转换为指定顺序的因子类型即可,具体操作如下:

操作方法

方法1:修改原数据的字段属性(最通用,后续所有调用该数据的绘图都会生效)

先指定Distance列的因子层级,层级顺序就是最终分面的排列顺序:

floral_data$Distance <- factor(floral_data$Distance, levels = c("Margin", "10 m", "50 m"))

再运行你原本的ggscatter绘图代码即可:

ggscatter(floral_data, x = "Spp Flowering", y = "Parasitoids", facet.by = "Distance",
          add = "reg.line",
          conf.int = TRUE,
          cor.coef = TRUE, # 添加相关系数
          cor.coeff.args = list(method = "spearman", label.y = 37, label.x = 5.7, label.sep = "\n"),
          xlab = "花缘平均开花物种数(5-6月)",
          ylab = "寄生蜂丰度") 

方法2:临时修改不改动原数据

借助dplyr的mutate在绘图流程中临时调整因子顺序:

library(dplyr)
floral_data %>%
  mutate(Distance = factor(Distance, levels = c("Margin", "10 m", "50 m"))) %>%
  ggscatter(x = "Spp Flowering", y = "Parasitoids", facet.by = "Distance",
            add = "reg.line",
            conf.int = TRUE,
            cor.coef = TRUE,
            cor.coeff.args = list(method = "spearman", label.y = 37, label.x = 5.7, label.sep = "\n"),
            xlab = "花缘平均开花物种数(5-6月)",
            ylab = "寄生蜂丰度")

原默认排序绘图结果

分面散点图结果

内容的提问来源于stack exchange,提问作者Bethany

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 05:36:04