当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。