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

R语言自定义散点图函数:合并关联参数的优化方法问询

简化R绘图函数的关联参数实现方法

我编写了一个用于绘制散点图的R自定义函数f_plot,代码如下:

f_plot = function(data, domain, y, z, x_title_label, y_title_label, timepoint) {
  
  item_dscrption = case_when(z == "ITEM1" ~ "Item 1 - Bloated Severity",
                             z == "ITEM2" ~ "Item 2 - Bloated Frequency",
                             z == "ITEM3" ~ "Item 3 - Eating",
                             z == "ITEM4" ~ "Item 4 - Tired",
                             z == "ITEM5" ~ "Item 5 - Breathing",
                             z == "ITEM6" ~ "Item 6 - Sleeping",
                             z == "ITEM7" ~ "Item 7 - Mobility",
                             z == "ITEM8" ~ "Item 8 - Comfort")
  
  domain_description = case_when(domain == "spring" ~ "Spring Season",
                                 domain == "summer" ~ "Summer Season",
                                 domain == "fall" ~ "Fall Season",
                                 domain == "winter" ~ "Winter Season")
  
  plot_data = data %>%
    filter(ABCD_domain == domain) 
  
  p = ggplot(plot_data, aes(x = ABCD_score, y = {{y}})) +
    geom_point() +
    geom_smooth(method = lm, se = FALSE, color = "#0433ff", size = 0.7) +
    ggpubr::stat_cor(aes(label = ..r.label..), label.x = 5.5, label.y = 4.5) +
    labs(x = paste0("Domain Score, ", domain_description, " (", timepoint, ")"),
         y = paste0(item_dscrption, " (", timepoint, ")")) +
    theme_bw()
  
  # Save the plot to a file (e.g., in PNG format)
  ggsave(filename = paste0("Item_", y_title_label, "_", domain, "_", timepoint, ".png"), plot = p, width = 4.5, height = 4.5, dpi = 500)
  
}

# 原调用方式
f_plot(data = data, domain = "spring", y = ITEM1, z = "ITEM1", y_title_label = "1", timepoint = "Baseline")

可以看到,函数中的y、z、y_title_label三个参数存在强关联:当y为ITEM1时,z需为"ITEM1",y_title_label为"1"。我希望将这3个关联参数简化为1个,请问是否有可行的实现方法?

测试数据集如下:

data <- data.frame(
  ID = c(
    "1002", "1002", "1002", "1002",
    "1006", "1006", "1006", "1006",
    "2170", "2170", "2170", "2170",
    "2166", "2166", "2166", "2166",
    "2168", "2168", "2168", "2168",
    "1003", "1003", "1003", "1003",
    "2169", "2169", "2169", "2169",
    "1005", "1005", "1005", "1005",
    "2165", "2165", "2165", "2165",
    "2171", "2171", "2171", "2171",
    "1004", "1004", "1004", "1004"
  ),
  ABCD_score = c(1, 0, 1, 0, 0, 1, 0, 1, 4, 3, 4, 3, 2, 4, 2, 4, 3, 2, 3, 3, 2, 3, 2, 0, 1, 1, 1, 1, 0, 3, 1, 3, 4, 3, 4, 2, 4, 1, 0, 2, 1, 1, 0, 3),
  ABCD_domain = c(
    "spring", "summer", "fall", "winter", "spring", "summer", "fall", "winter",
    "spring", "summer", "fall", "winter", "spring", "summer", "fall", "winter",
    "spring", "summer", "fall", "winter", "spring", "summer", "fall", "winter",
    "spring", "summer", "fall", "winter", "spring", "summer", "fall", "winter",
    "spring", "summer", "fall", "winter", "spring", "summer", "fall", "winter",
    "spring", "summer", "fall", "winter"
  ),
  ITEM1 = c(1, 1, 1, 1, 0, 0, 0, 0, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2)
)

解决方案:通过变量名提取+预定义映射表实现参数简化

核心思路是:利用rlang包的工具提取y参数的字符串名称,再通过预定义的映射表自动获取对应的描述和标题标签,无需手动传入关联参数。

修改后的函数代码:

library(rlang)
library(dplyr)
library(ggplot2)
library(ggpubr)

f_plot = function(data, domain, y, timepoint) {
  # 预定义ITEM映射表:包含名称、描述、标题标签
  item_map <- tibble(
    item_name = paste0("ITEM", 1:8),
    item_desc = c(
      "Item 1 - Bloated Severity",
      "Item 2 - Bloated Frequency",
      "Item 3 - Eating",
      "Item 4 - Tired",
      "Item 5 - Breathing",
      "Item 6 - Sleeping",
      "Item 7 - Mobility",
      "Item 8 - Comfort"
    ),
    title_label = as.character(1:8)
  )
  
  # 提取y参数的字符串名称
  y_name <- as_name(enquo(y))
  
  # 从映射表中匹配对应的描述和标题标签
  item_info <- item_map %>% filter(item_name == y_name)
  item_dscrption <- item_info$item_desc
  y_title_label <- item_info$title_label
  
  # 季节描述映射
  domain_description <- case_when(
    domain == "spring" ~ "Spring Season",
    domain == "summer" ~ "Summer Season",
    domain == "fall" ~ "Fall Season",
    domain == "winter" ~ "Winter Season"
  )
  
  plot_data = data %>%
    filter(ABCD_domain == domain) 
  
  p = ggplot(plot_data, aes(x = ABCD_score, y = {{y}})) +
    geom_point() +
    geom_smooth(method = lm, se = FALSE, color = "#0433ff", size = 0.7) +
    ggpubr::stat_cor(aes(label = ..r.label..), label.x = 5.5, label.y = 4.5) +
    labs(x = paste0("Domain Score, ", domain_description, " (", timepoint, ")"),
         y = paste0(item_dscrption, " (", timepoint, ")")) +
    theme_bw()
  
  # 保存图片
  ggsave(filename = paste0("Item_", y_title_label, "_", domain, "_", timepoint, ".png"), 
         plot = p, width = 4.5, height = 4.5, dpi = 500)
  
}

# 简化后的调用方式
f_plot(data = data, domain = "spring", y = ITEM1, timepoint = "Baseline")

关键说明:

  • 提取变量名:enquo(y)将传入的y参数转为引用,as_name()把引用转为字符串名称,自动获取ITEM1这样的字符串,代替手动传入z。
  • 预定义映射表:用tibble创建统一的ITEM映射关系,比多个case_when更简洁易维护,后续新增ITEM只需在映射表中添加一行即可。
  • 参数简化:现在调用函数时只需传入y参数,z和y_title_label对应的信息会自动从映射表中匹配,避免手动输入错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 10:37:13