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

如何用ggplot2绘制三组不同的非线性回归曲线

Fixing ggplot2 LOESS Fit Line Styling Issues (Dashed Lines, Color Matching, Extra Legends)

Let's break down and fix the styling problems you're facing with your ggplot2 plot:

Key Issues in Your Current Code

  1. Putting linetype=group in the global aes() makes all geoms (including errorbars) inherit this mapping, which is why your errorbars turned dashed.
  2. The fill=group in stat_smooth() is unnecessary (since you set se=F) and creates an extra legend.
  3. The legend for linetype is separate from color/shape, leading to redundant legends.

Corrected Code

ggplot(datapoidsmono, aes(x = time, y = weight, group = group, color = group)) +
  # Error bars (keep solid, inherit color from group)
  stat_summary(fun.data = "mean_sdl", fun.args = list(mult=1), 
               geom = "errorbar", position = "identity", 
               size = 0.5, width = 0.2, linetype = 1) +
  # Mean points (shape mapped to group)
  stat_summary(fun.y = "mean", geom = "point", size = 3, 
               aes(shape = group)) +
  # LOESS fit lines: dashed, mapped to group, no confidence band
  stat_smooth(method = "loess", se = F, 
              aes(linetype = group), size = 0.8) +
  # Scales: align labels and values across color/shape/linetype
  scale_x_discrete(name = "Days after injection") +
  scale_y_continuous(name = "Weight (g)", limits = c(0, 4000), breaks = seq(0, 4000, 500)) +
  scale_color_manual(values = c("green", "blue", "red"), 
                     name = "Treatment", labels = c("A", "B", "C")) +
  scale_shape_manual(values = c(15, 16, 17), 
                     name = "Treatment", labels = c("A", "B", "C")) +
  scale_linetype_manual(values = c("dotted", "dotted", "dotted"), 
                        name = "Treatment", labels = c("A", "B", "C")) +
  # Theme and title settings
  ggtitle("Weight variation over time") +
  theme(
    plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),
    legend.position = "right",
    legend.background = element_rect(size = 0.5, linetype = "solid", color = "black", fill = "white"),
    axis.line.x = element_line(size = 0.5, color = "black"),
    axis.text.x = element_text(color = "black", size = 12),
    axis.line.y = element_line(size = 0.5, color = "black"),
    axis.text.y = element_text(color = "black", size = 12),
    axis.title = element_text(size = 15, face = "bold"),
    panel.grid.major = element_line(color = "#F1F1F1"),
    panel.grid.minor = element_blank(),
    panel.background = element_blank()
  ) +
  # Merge legends: ensure color/shape/linetype share one legend
  guides(color = guide_legend(override.aes = list(shape = c(15,16,17), linetype = c("dotted","dotted","dotted"))),
         shape = "none",
         linetype = "none")

Key Adjustments Explained

  1. Moved linetype=group to stat_smooth() only: This ensures only the fit lines use dashed styles, while errorbars remain solid (we explicitly set linetype=1 for errorbars to be safe).
  2. Removed unnecessary fill=group from stat_smooth(): Since se=F, there's no fill to map, so this was creating an extra legend we don't need.
  3. Aligned scale names/labels: All scale_*_manual() functions use the same name and labels to ensure legend consistency.
  4. Merged legends: The guides() call combines color, shape, and linetype into a single "Treatment" legend, hiding the redundant shape/linetype legends.
  5. Explicitly set method="loess": While ggplot2 uses LOESS by default for small datasets, making it explicit makes your code more readable.

This should give you:

  • Solid error bars matching group colors
  • Mean points with unique shapes and group colors
  • Dotted LOESS fit lines matching each group's color
  • A single, clean legend for all treatment groups

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:20:33