默认离散色阶。默认为scale_fill_hue() /scale_fill_brewer(),除非指定type(默认为ggplot2.discrete.fill /ggplot2.discrete.colour 选项)。
用法
scale_colour_discrete(..., type = getOption("ggplot2.discrete.colour"))
scale_fill_discrete(..., type = getOption("ggplot2.discrete.fill"))
参数
- ...
 - 
传递给比例类型的附加参数,
 - type
 - 
以下之一:
- 
颜色代码的字符向量。只要代码的数量超过数据级别的数量,代码就用于'manual'色标(如果级别多于代码,则使用
scale_colour_hue()/scale_fill_hue()构建默认标度)。如果这是一个命名向量,则颜色值将根据向量名称与级别进行匹配。不匹配的数据值将设置为na.value。 - 
颜色代码的字符向量列表。选择超过数据级别数量的最小长度向量用于颜色缩放。如果您想根据级别数更改调色板,这非常有用。
 - 
返回离散颜色/填充比例的函数(例如
scale_fill_hue()、scale_fill_brewer()等)。 
 - 
 
例子
# Template function for creating densities grouped by a variable
cty_by_var <- function(var) {
  ggplot(mpg, aes(cty, colour = factor({{var}}), fill = factor({{var}}))) +
    geom_density(alpha = 0.2)
}
# The default, scale_fill_hue(), is not colour-blind safe
cty_by_var(class)
# (Temporarily) set the default to Okabe-Ito (which is colour-blind safe)
okabe <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
withr::with_options(
  list(ggplot2.discrete.fill = okabe),
  print(cty_by_var(class))
)
# Define a collection of palettes to alter the default based on number of levels to encode
discrete_palettes <- list(
  c("skyblue", "orange"),
  RColorBrewer::brewer.pal(3, "Set2"),
  RColorBrewer::brewer.pal(6, "Accent")
)
withr::with_options(
  list(ggplot2.discrete.fill = discrete_palettes), {
  # 1st palette is used when there 1-2 levels (e.g., year)
  print(cty_by_var(year))
  # 2nd palette is used when there are 3 levels
  print(cty_by_var(drv))
  # 3rd palette is used when there are 4-6 levels
  print(cty_by_var(fl))
})
#> Warning: Groups with fewer than two data points have been dropped.
#> Warning: no non-missing arguments to max; returning -Inf
相关用法
- R ggplot2 scale_colour_continuous 连续色标和分级色标
 - R ggplot2 scale_continuous 连续数据的位置比例(x 和 y)
 - R ggplot2 scale_gradient 渐变色阶
 - R ggplot2 scale_shape 形状比例,又称字形
 - R ggplot2 scale_viridis 来自 viridisLite 的 Viridis 色标
 - R ggplot2 scale_grey 连续灰度色阶
 - R ggplot2 scale_linetype 线条图案的比例
 - R ggplot2 scale_discrete 离散数据的位置尺度
 - R ggplot2 scale_manual 创建您自己的离散尺度
 - R ggplot2 scale_steps 分级渐变色标
 - R ggplot2 scale_size 面积或半径比例
 - R ggplot2 scale_date 日期/时间数据的位置刻度
 - R ggplot2 scale_binned 用于对连续数据进行装箱的位置比例(x 和 y)
 - R ggplot2 scale_alpha Alpha 透明度比例
 - R ggplot2 scale_identity 使用不缩放的值
 - R ggplot2 scale_linewidth 线宽比例
 - R ggplot2 scale_hue 离散数据的均匀间隔颜色
 - R ggplot2 scale_brewer ColorBrewer 的连续、发散和定性色标
 - R ggplot2 stat_ellipse 计算法行数据椭圆
 - R ggplot2 stat_identity 保留数据原样
 - R ggplot2 stat_summary_2d 以二维形式进行分类和汇总(矩形和六边形)
 - R ggplot2 should_stop 在示例中用于说明何时应该发生错误。
 - R ggplot2 stat_summary 总结唯一/分箱 x 处的 y 值
 - R ggplot2 stat_sf_coordinates 从“sf”对象中提取坐标
 - R ggplot2 stat_unique 删除重复项
 
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Discrete colour scales。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
