简体   繁体   English

为什么 scale_linetype_manual() 会巧妙地改变线型美学?

[英]Why does scale_linetype_manual() subtly change the linetype aesthetic?

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.这些不一样。

  1. Why not?为什么不?

  2. 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 

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 .线型调色板由scales::linetype_palette生成。 The first 3 values are:前 3 个值是:

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)reprex 包于 2022-06-22 创建 (v2.0.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM