Use package ggnewscale
, function new_scale_colour
to replace the existing, active scale with another one. See also this SO post and this R-bloggers post .
Two notes:
- 线条宽度较大,以使点更明显;
- 我已经更改了线和点层的顺序,以便线不会叠加点.
library(tidyverse)
library(ggnewscale)
clr_lines <- setNames(c("green", "cyan", "magenta"), c("4", "f", "r"))
clr_points <- setNames(c("red", "black", "yellow"), c("4", "f", "r"))
ggplot(mpg) +
geom_line(aes(x = displ, y = cty, color = drv), linewidth = 3) +
scale_color_manual(values = clr_lines) +
# start a new scale
new_scale_colour() +
geom_point(aes(x = displ, y = cty, color = drv)) +
scale_color_manual(values = clr_points)
创建于2023-12-21,共reprex v2.0.2个