如何用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
- Putting
linetype=groupin the globalaes()makes all geoms (including errorbars) inherit this mapping, which is why your errorbars turned dashed. - The
fill=groupinstat_smooth()is unnecessary (since you setse=F) and creates an extra legend. - 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
- Moved
linetype=grouptostat_smooth()only: This ensures only the fit lines use dashed styles, while errorbars remain solid (we explicitly setlinetype=1for errorbars to be safe). - Removed unnecessary
fill=groupfromstat_smooth(): Sincese=F, there's no fill to map, so this was creating an extra legend we don't need. - Aligned scale names/labels: All
scale_*_manual()functions use the samenameandlabelsto ensure legend consistency. - Merged legends: The
guides()call combines color, shape, and linetype into a single "Treatment" legend, hiding the redundant shape/linetype legends. - 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_
相关产品推荐
相关产品推荐

