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

如何使用ggplot绘制含标准差区域的双列表可视化图表?

Got it, let's get you that standard deviation shaded area plot using ggplot2! Here's a step-by-step breakdown tailored to your data structure:

Step 1: Prepare Your Data for ggplot2

First, we need to convert your nested list structure into a "tidy" data frame—this is the format ggplot2 works best with. Let's start by simulating a dataset that matches your structure (you can swap this out with your actual data):

# Simulate your data: 2 groups, each with 6 replicate vectors of length 75
set.seed(123) # Ensures reproducible results
my_data <- list(
  Group_A = replicate(6, rnorm(75, mean = 0.9, sd = 0.02), simplify = FALSE),
  Group_B = replicate(6, rnorm(75, mean = 0.85, sd = 0.03), simplify = FALSE)
)

Now let's unpack this nested list into a tidy data frame with columns for group, replicate values, and x-axis positions (since each vector is length 75, we'll use 1 to 75 as our x values):

library(tidyverse) # Loads ggplot2, dplyr, tidyr, etc.

tidy_data <- my_data %>%
  enframe(name = "Group", value = "Replicate_Vals") %>% # Label outer list items as groups
  unnest(Replicate_Vals) %>% # Expand each group's 6 replicates
  group_by(Group) %>%
  mutate(X = rep(1:75, times = n()/75)) %>% # Assign x positions (1-75) to each value
  ungroup() %>%
  rename(Y = Replicate_Vals) # Rename the numeric column for clarity

Step 2: Calculate Mean and Standard Deviation

Next, we'll compute the mean and standard deviation for each group at every x position:

summary_stats <- tidy_data %>%
  group_by(Group, X) %>%
  summarise(
    Mean_Y = mean(Y),
    SD_Y = sd(Y),
    .groups = "drop" # Remove grouping after calculation
  )

Step 3: Plot with ggplot2

Now we can build the plot—we'll use geom_ribbon for the shaded standard deviation area, and geom_line for the mean curve:

ggplot(summary_stats, aes(x = X, y = Mean_Y, color = Group)) +
  # Shaded area: mean ± standard deviation
  geom_ribbon(
    aes(ymin = Mean_Y - SD_Y, ymax = Mean_Y + SD_Y, fill = Group),
    alpha = 0.2, # Adjust transparency so the line stays visible
    color = NA # Remove border around the shaded area
  ) +
  # Line for the mean value
  geom_line(linewidth = 1) +
  # Customize labels and theme
  labs(
    x = "X Position",
    y = "Value",
    title = "Mean Values with Standard Deviation Shaded Regions"
  ) +
  theme_minimal()

Key Notes:

  • If your groups have different names than the example, just update the names in your original list—enframe will pick them up automatically.
  • Adjust alpha to make the shaded area more or less transparent, and linewidth to change the thickness of the mean line.
  • If your x-axis isn't just 1-75 (e.g., it's time points or another metric), replace rep(1:75, ...) with your actual x values.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:46:52