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

如何用RStudio的geom_line与geom_area绘制对比折线填充图并修复异常?

修复RStudio中ggplot2折线填充图异常问题

问题说明

需要使用geom_line和geom_area绘制两组数据的对比折线填充图,但当前代码生成的图像不符合预期,目标是得到两组数据在同一坐标系下的对比填充折线图。

当前代码

ggplot(data, aes(Strain, Stress, colour=Treatment))+ 
  geom_point(aes(), size= 1.5, linetype="dotted")+ 
  geom_area(aes(), alpha=0.2)+ 
  xlim(0,0.01)+ylim(0,0.003)+ 
  theme(
    line = element_line(colour = "black", size = 1, linetype = 1, lineend = "butt"),
    rect = element_rect(fill = "white", colour = "black", size = 1, linetype = 1),
    aspect.ratio = 1,
    plot.background = element_rect(fill = "white"),
    plot.margin = margin(1, 1, 1, 1, "cm"),
    axis.text = element_text(size = rel(2.5), colour = "#000000", margin = 1), 
    strip.text = element_text(size = rel(0.8)), 
    axis.line = element_blank(), 
    axis.text.x = element_text(vjust = 0.2), 
    axis.text.y = element_text(hjust = 1), 
    axis.ticks = element_line(colour = "#000000", size = 1.2), 
    axis.title.x = element_text(size = 30, vjust=0.5), 
    axis.title.y = element_text(size = 30, angle = 90), 
    axis.ticks.length = unit(0.15, "cm"), 
    legend.background = element_rect(colour = NA), 
    legend.margin = unit(0.15, "cm"), 
    legend.key = element_rect(fill = "grey95", colour = "white"), 
    legend.key.size = unit(1.2, "lines"), 
    legend.key.height = NULL, 
    legend.key.width = NULL, 
    legend.text = element_text(size = rel(2.0)), 
    legend.text.align = NULL, 
    legend.title = element_text(size = rel(2.0), face = "bold", hjust = 0), 
    legend.title.align = NULL, 
    legend.position = "bottom", 
    legend.direction = NULL,
    legend.justification = "center",
    legend.box = NULL, 
    panel.background = element_rect(fill = "#ffffff", colour = "#000000",
                                  size = 2, linetype = "solid"), 
    panel.border = element_rect(colour = "black", fill=NA, size=2), 
  )+ 
  ylab("Stress(MPa)") + 
  xlab("Strain")

图像对比

当前异常效果

当前生成的异常图像

预期目标效果

预期的折线填充对比图

修复步骤

  • 补充分组与填充映射:geom_area需要通过fill=Treatment区分两组填充颜色,同时确保折线和填充都按Treatment分组。修改主图层的aes,添加group=Treatment, fill=Treatment。
  • 替换散点为折线:当前代码使用geom_point绘制散点,需添加geom_line()来生成折线,若不需要散点可直接移除geom_point层。
  • 排序数据:若数据中Strain未按从小到大排序,会导致折线乱序。绘图前对数据排序:
    data <- data %>% arrange(Treatment, Strain)
    
  • 调整填充位置:使用position="identity"确保两组填充区域独立显示(默认即为该值,若之前被修改需手动指定)。

修改后的核心代码示例

# 先对数据按分组和应变排序
library(dplyr)
data <- data %>% arrange(Treatment, Strain)

# 绘制折线填充图
ggplot(data, aes(x = Strain, y = Stress, colour = Treatment, group = Treatment, fill = Treatment)) +
  geom_line(size = 1) +  # 绘制折线
  geom_area(alpha = 0.2, position = "identity") +  # 填充区域
  xlim(0, 0.01) +
  ylim(0, 0.003) +
  # 保留原主题设置
  theme(
    line = element_line(colour = "black", size = 1, linetype = 1, lineend = "butt"),
    rect = element_rect(fill = "white", colour = "black", size = 1, linetype = 1),
    aspect.ratio = 1,
    plot.background = element_rect(fill = "white"),
    plot.margin = margin(1, 1, 1, 1, "cm"),
    axis.text = element_text(size = rel(2.5), colour = "#000000", margin = 1), 
    strip.text = element_text(size = rel(0.8)), 
    axis.line = element_blank(), 
    axis.text.x = element_text(vjust = 0.2), 
    axis.text.y = element_text(hjust = 1), 
    axis.ticks = element_line(colour = "#000000", size = 1.2), 
    axis.title.x = element_text(size = 30, vjust=0.5), 
    axis.title.y = element_text(size = 30, angle = 90), 
    axis.ticks.length = unit(0.15, "cm"), 
    legend.background = element_rect(colour = NA), 
    legend.margin = unit(0.15, "cm"), 
    legend.key = element_rect(fill = "grey95", colour = "white"), 
    legend.key.size = unit(1.2, "lines"), 
    legend.key.height = NULL, 
    legend.key.width = NULL, 
    legend.text = element_text(size = rel(2.0)), 
    legend.text.align = NULL, 
    legend.title = element_text(size = rel(2.0), face = "bold", hjust = 0), 
    legend.title.align = NULL, 
    legend.position = "bottom", 
    legend.direction = NULL,
    legend.justification = "center",
    legend.box = NULL, 
    panel.background = element_rect(fill = "#ffffff", colour = "#000000",
                                  size = 2, linetype = "solid"), 
    panel.border = element_rect(colour = "black", fill=NA, size=2), 
  ) +
  ylab("Stress(MPa)") +
  xlab("Strain")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:46:17