當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R ggplot2 scale_viridis 來自 viridisLite 的 Viridis 色標


viridis 比例提供顏色和 black-and-white 感知上均一的顏色圖。它們還旨在讓患有常見色盲的觀眾能夠感知。另請參閱https://bids.github.io/colormap/

用法

scale_colour_viridis_d(
  ...,
  alpha = 1,
  begin = 0,
  end = 1,
  direction = 1,
  option = "D",
  aesthetics = "colour"
)

scale_fill_viridis_d(
  ...,
  alpha = 1,
  begin = 0,
  end = 1,
  direction = 1,
  option = "D",
  aesthetics = "fill"
)

scale_colour_viridis_c(
  ...,
  alpha = 1,
  begin = 0,
  end = 1,
  direction = 1,
  option = "D",
  values = NULL,
  space = "Lab",
  na.value = "grey50",
  guide = "colourbar",
  aesthetics = "colour"
)

scale_fill_viridis_c(
  ...,
  alpha = 1,
  begin = 0,
  end = 1,
  direction = 1,
  option = "D",
  values = NULL,
  space = "Lab",
  na.value = "grey50",
  guide = "colourbar",
  aesthetics = "fill"
)

scale_colour_viridis_b(
  ...,
  alpha = 1,
  begin = 0,
  end = 1,
  direction = 1,
  option = "D",
  values = NULL,
  space = "Lab",
  na.value = "grey50",
  guide = "coloursteps",
  aesthetics = "colour"
)

scale_fill_viridis_b(
  ...,
  alpha = 1,
  begin = 0,
  end = 1,
  direction = 1,
  option = "D",
  values = NULL,
  space = "Lab",
  na.value = "grey50",
  guide = "coloursteps",
  aesthetics = "fill"
)

參數

...

其他參數傳遞給 discrete_scale()continuous_scale()binned_scale() 來控製名稱、限製、中斷、標簽等。

alpha

Alpha 透明度,[0,1] 中的數字,請參閱hsv 中的參數 alpha。

begin, end

[0,1] 中顏色圖開始和結束的(校正後的)色調。

direction

設置比例中顏色的順序。如果為 1(默認值),則顏色按從最深到最亮的順序排列。如果為 -1,則顏色順序相反。

option

指示要使用的顏色圖選項的字符串。有八個選項可供選擇:

  • "magma"(或"A")

  • "inferno"(或"B")

  • "plasma"(或"C")

  • "viridis"(或"D")

  • "cividis"(或"E")

  • "rocket"(或"F")

  • "mako"(或"G")

  • "turbo"(或"H")

aesthetics

字符串或字符串向量,列出了該比例所使用的美學名稱。例如,這可以用於通過 aesthetics = c("colour", "fill") 同時將顏色設置應用於 colourfill 美學。

values

如果顏色不應該沿著漸變均勻定位,則該向量給出 colours 向量中每種顏色的位置(0 到 1 之間)。有關將任意範圍映射到 0 到 1 之間的便捷函數,請參閱 rescale()

space

用於計算漸變的顏色空間。必須是 "Lab" - 其他值已棄用。

na.value

缺失值將替換為該值。

guide

用於創建指南或其名稱的函數。有關詳細信息,請參閱guides()

例子

# viridis is the default colour/fill scale for ordered factors
set.seed(596)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
ggplot(dsamp, aes(carat, price)) +
  geom_point(aes(colour = clarity))


# Use viridis_d with discrete data
txsamp <- subset(txhousing, city %in%
  c("Houston", "Fort Worth", "San Antonio", "Dallas", "Austin"))
(d <- ggplot(data = txsamp, aes(x = sales, y = median)) +
   geom_point(aes(colour = city)))

d + scale_colour_viridis_d()


# Change scale label
d + scale_colour_viridis_d("City\nCenter")


# Select palette to use, see ?scales::viridis_pal for more details
d + scale_colour_viridis_d(option = "plasma")

d + scale_colour_viridis_d(option = "inferno")


# scale_fill_viridis_d works just the same as
# scale_colour_viridis_d but for fill colours
p <- ggplot(txsamp, aes(x = median, fill = city)) +
  geom_histogram(position = "dodge", binwidth = 15000)
p + scale_fill_viridis_d()

# the order of colour can be reversed
p + scale_fill_viridis_d(direction = -1)


# Use viridis_c with continous data
(v <- ggplot(faithfuld) +
  geom_tile(aes(waiting, eruptions, fill = density)))

v + scale_fill_viridis_c()

v + scale_fill_viridis_c(option = "plasma")


# Use viridis_b to bin continuous data before mapping
v + scale_fill_viridis_b()


相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Viridis colour scales from viridisLite。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。