I want to manually specify the linetypes for a fixed ordering of factors (so changing the underlying factor ordering is not an option).
library(ggplot2)
df = data.frame(x = rep(1:10, 3),
y = c(runif(10), runif(10), runif(10)),
z = c(rep("a", 10), rep("b", 10), rep("c", 10)))
ggplot(df, aes(x, y, linetype = z)) geom_line()
ggplot(df, aes(x, y, linetype = z))
geom_line()
scale_linetype_manual(values = c("solid", "dotted", "dashed"))
These are not the same.
Why not?
How do I make them the same?
R version 4.1.2 (2021-11-01)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.4
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
other attached packages:
[1] ggplot2_3.3.5
CodePudding user response:
The simple answer is that the three linetypes you have chosen are not the default line types. The linetype palette is generated by scales::linetype_palette
. The first 3 values are:
scales::linetype_pal()(3)
#> [1] "solid" "22" "42"
So you can replicate the default by doing:
ggplot(df, aes(x, y, linetype = z))
geom_line()
theme_minimal()
scale_linetype_manual(values = scales::linetype_pal()(3))
Created on 2022-06-22 by the reprex package (v2.0.1)