這些函數允許您指定自己的一組從數據級別到美學值的映射。
用法
scale_colour_manual(
  ...,
  values,
  aesthetics = "colour",
  breaks = waiver(),
  na.value = "grey50"
)
scale_fill_manual(
  ...,
  values,
  aesthetics = "fill",
  breaks = waiver(),
  na.value = "grey50"
)
scale_size_manual(..., values, breaks = waiver(), na.value = NA)
scale_shape_manual(..., values, breaks = waiver(), na.value = NA)
scale_linetype_manual(..., values, breaks = waiver(), na.value = "blank")
scale_linewidth_manual(..., values, breaks = waiver(), na.value = NA)
scale_alpha_manual(..., values, breaks = waiver(), na.value = NA)
scale_discrete_manual(aesthetics, ..., values, breaks = waiver())
參數
- ...
 - 
參數傳遞給
discrete_scalepalette- 
調色板函數,當使用單個整數參數(比例中的級別數)調用時,返回它們應采用的值(例如
scales::hue_pal())。 limits- 
之一:
- 
NULL使用默認比例值 - 
定義可能的比例值及其順序的字符向量
 - 
接受現有(自動)值並返回新值的函數。還接受 rlang lambda 函數表示法。
 
 - 
 drop- 
是否應該從量表中省略未使用的因子水平?默認值
TRUE使用數據中出現的級別;FALSE使用因子中的所有級別。 na.translate- 
與連續尺度不同,離散尺度可以輕鬆顯示缺失值,並且默認情況下會這樣做。如果要從離散尺度中刪除缺失值,請指定
na.translate = FALSE。 scale_name- 
應用於與該比例關聯的錯誤消息的比例名稱。
 name- 
秤的名稱。用作軸或圖例標題。如果
waiver()(默認值),則比例名稱取自用於該美學的第一個映射。如果是NULL,則圖例標題將被省略。 labels- 
之一:
 guide- 
用於創建指南或其名稱的函數。有關詳細信息,請參閱
guides()。 super- 
用於構造比例的超類
 
 - values
 - 
將數據值映射到的一組美學值。這些值將按順序(通常按字母順序)與比例限製匹配,或者與
breaks(如果提供)匹配。如果這是一個命名向量,則將根據名稱來匹配值。不匹配的數據值將被賦予na.value。 - aesthetics
 - 
字符串或字符串向量,列出了該比例所使用的美學名稱。例如,這可以用於通過
aesthetics = c("colour", "fill")同時將顏色設置應用於colour和fill美學。 - breaks
 - 
之一:
- 
NULL不間斷 - 
waiver()用於默認中斷(比例限製) - 
中斷的特征向量
 - 
將限製作為輸入並返回中斷作為輸出的函數
 
 - 
 - na.value
 - 
用於缺失 (
NA) 值的美學值 
細節
函數 scale_colour_manual() 、 scale_fill_manual() 、 scale_size_manual() 等適用於比例名稱中指定的美觀: colour 、 fill 、 size 等。但是,函數 scale_colour_manual() 和 scale_fill_manual() 還有一個可選的 aesthetics 參數,可用於通過單個函數調用定義 colour 和 fill 美學映射(請參閱示例)。函數 scale_discrete_manual() 是一個通用尺度,可以與通過 aesthetics 參數提供的任何美學或一組美學一起使用。
色盲
許多源自 RGB 組合的調色板(例如 "rainbow" 調色板)並不適合支持所有觀看者,尤其是那些有色覺缺陷的觀看者。使用 viridis 類型(顏色和 black-and-white 顯示在感知上一致)是確保可視化具有良好感知屬性的簡單選擇。色彩空間包提供的函數
- 
生成具有良好感知特性的調色板,
 - 
分析給定的調色板,例如模擬色盲,
 - 
並修改給定的調色板以獲得更好的感知能力。
 
有關色覺缺陷和合適的顏色選擇的更多信息,請參閱paper on the colorspace package 及其參考文獻。
例子
p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(colour = factor(cyl)))
p + scale_colour_manual(values = c("red", "blue", "green"))
# It's recommended to use a named vector
cols <- c("8" = "red", "4" = "blue", "6" = "darkgreen", "10" = "orange")
p + scale_colour_manual(values = cols)
# You can set color and fill aesthetics at the same time
ggplot(
  mtcars,
  aes(mpg, wt, colour = factor(cyl), fill = factor(cyl))
) +
  geom_point(shape = 21, alpha = 0.5, size = 2) +
  scale_colour_manual(
    values = cols,
    aesthetics = c("colour", "fill")
  )
# As with other scales you can use breaks to control the appearance
# of the legend.
p + scale_colour_manual(values = cols)
p + scale_colour_manual(
  values = cols,
  breaks = c("4", "6", "8"),
  labels = c("four", "six", "eight")
)
# And limits to control the possible values of the scale
p + scale_colour_manual(values = cols, limits = c("4", "8"))
p + scale_colour_manual(values = cols, limits = c("4", "6", "8", "10"))
相關用法
- 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_colour_discrete 離散色階
 - R ggplot2 scale_steps 分級漸變色標
 - R ggplot2 scale_size 麵積或半徑比例
 - R ggplot2 scale_date 日期/時間數據的位置刻度
 - R ggplot2 scale_continuous 連續數據的位置比例(x 和 y)
 - R ggplot2 scale_binned 用於對連續數據進行裝箱的位置比例(x 和 y)
 - R ggplot2 scale_alpha Alpha 透明度比例
 - R ggplot2 scale_colour_continuous 連續色標和分級色標
 - 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等大神的英文原創作品 Create your own discrete scale。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
