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

如何修改plot_function()指标及让plot_model()显示均值与标准差?

问题解决方案

一、修改plot_function()中的指标

针对自定义的plot_function(),根据需求调整核心逻辑:

  • 更换指标变量:如果要切换展示的变量,直接修改函数内ggplot()的aes()映射变量名。示例:
    # 原函数(示例)
    plot_function <- function(data) {
      ggplot(data, aes(x = Condition, y = Old_Var)) + geom_line()
    }
    # 修改后
    plot_function <- function(data) {
      ggplot(data, aes(x = Condition, y = New_Var)) + geom_line()
    }
    
  • 更换统计量:如果要替换展示的统计指标(如从均值换成中位数/标准差),修改统计量计算代码。示例:
    # 原函数计算均值
    plot_function <- function(data) {
      sum_data <- data %>% group_by(Condition) %>% summarise(stat = mean(Var))
      ggplot(sum_data, aes(x = Condition, y = stat)) + geom_col()
    }
    # 修改为计算标准差
    plot_function <- function(data) {
      sum_data <- data %>% group_by(Condition) %>% summarise(stat = sd(Var))
      ggplot(sum_data, aes(x = Condition, y = stat)) + geom_col()
    }
    

二、让plot_model()展示均值与标准差

sjPlot::plot_model(type = "pred")默认展示模型预测值的置信区间,要改为展示原始数据的均值±标准差,可按以下两种方法操作:

方法1:在现有plot_model绘图基础上叠加标准差

  1. 先从原始数据中按分组计算均值和标准差:
    library(dplyr)
    # 替换"Response_Var"为你的模型实际响应变量名
    summary_data <- Focus_full %>%
      group_by(Condition, Group) %>%
      summarise(
        mean_y = mean(Response_Var, na.rm = TRUE),
        sd_y = sd(Response_Var, na.rm = TRUE),
        .groups = "drop"
      )
    
  2. 在原有绘图代码后叠加误差线图层:
    plot_Focus <- plot_model(fit, type = "pred", terms = c("Condition", "Group")) +
      coord_cartesian(xlim = c(0.5, NA), ylim = c(0,5), clip = "off") + 
      theme_tq() + scale_colour_tq() + scale_fill_tq(light) + 
      labs(title = "", y = "", x = "") +  
      ggplot2::labs(colour = "Group") + scale_color_manual(values=c('Red','Black')) +
      # 新增:叠加均值±标准差的误差线
      geom_pointrange(data = summary_data,
                      aes(x = Condition, y = mean_y, 
                          ymin = mean_y - sd_y, ymax = mean_y + sd_y,
                          color = Group),
                      position = position_dodge(width = 0.2)) +
      theme_apa()
    

方法2:直接用原始统计数据绘图(更直接)

如果不需要模型预测线,可直接基于计算好的均值和标准差绘图:

library(ggplot2)
library(dplyr)
library(ggthemes) # 用于theme_tq、theme_apa

summary_data <- Focus_full %>%
  group_by(Condition, Group) %>%
  summarise(
    mean_y = mean(Response_Var, na.rm = TRUE),
    sd_y = sd(Response_Var, na.rm = TRUE),
    .groups = "drop"
  )

plot_Focus <- ggplot(summary_data, aes(x = Condition, y = mean_y, color = Group)) +
  geom_line(position = position_dodge(width = 0.2)) +
  geom_pointrange(aes(ymin = mean_y - sd_y, ymax = mean_y + sd_y),
                  position = position_dodge(width = 0.2)) +
  coord_cartesian(xlim = c(0.5, NA), ylim = c(0,5), clip = "off") +
  theme_tq() +
  scale_color_manual(values=c('Red','Black')) +
  labs(colour = "Group", y = "", x = "") +
  theme_apa()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 00:25:30