plotly 中的自定义离散色标
Custom discrete color scale in plotly
我想自定义 plotly
图中的颜色。根据 docs:
这适用于连续变量和尺度
library(plotly)
plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = Sepal.Length, colors = c("#132B43", "#56B1F7"),
mode = "markers")
但是,如果我将参数设置为离散颜色(字符或因子),这仍然有效但会引发警告:
> plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = Sepal.Length>6, colors = c("#132B43", "#56B1F7"),
mode = "markers")
Warning message:
In RColorBrewer::brewer.pal(N, "Set2") :
minimal value for n is 3, returning requested palette with 3 different levels
如何正确执行此操作?
这不是情节问题,而是 ColorBrewer(以及相关的 RColorBrewer
包)的设计特征。您会注意到,当您将 color
分配给等于或超过三个级别的因素时,警告会消失,例如
plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = cut(Sepal.Length, 3), colors = "Set1",
mode = "markers")
这是因为 ColorBrewer 的最小数据数 类 是三个(从 http://colorbrewer2.org/ 可以看出,其中不能 select 少于三个 类)。比如在?brewer.pal
(plotly引用的函数)中,具体写着
All the sequential palettes are available in variations from 3
different values up to 9 different values.
[...]
For qualitative palettes, the lowest number of distinct values available always is 3
由于 build_plotly()
(函数 plotly()
在内部调用)总是调用 brewer.pal()
(参见第 474 行 here),如果不重写 build_plotly()
函数不调用 brewer.pal()
少于 3 个数据 类。
同时,要关闭警告,将绘图输出分配给一个对象并将 print(object)
语句包装在 suppressWarnings()
中,如下所示:
plotly_plot <- plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = Sepal.Length>6, colors = c("#132B43", "#56B1F7"),
mode = "markers")
suppressWarnings(print(plotly_plot))
我想自定义 plotly
图中的颜色。根据 docs:
library(plotly)
plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = Sepal.Length, colors = c("#132B43", "#56B1F7"),
mode = "markers")
但是,如果我将参数设置为离散颜色(字符或因子),这仍然有效但会引发警告:
> plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = Sepal.Length>6, colors = c("#132B43", "#56B1F7"),
mode = "markers")
Warning message:
In RColorBrewer::brewer.pal(N, "Set2") :
minimal value for n is 3, returning requested palette with 3 different levels
如何正确执行此操作?
这不是情节问题,而是 ColorBrewer(以及相关的 RColorBrewer
包)的设计特征。您会注意到,当您将 color
分配给等于或超过三个级别的因素时,警告会消失,例如
plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = cut(Sepal.Length, 3), colors = "Set1",
mode = "markers")
这是因为 ColorBrewer 的最小数据数 类 是三个(从 http://colorbrewer2.org/ 可以看出,其中不能 select 少于三个 类)。比如在?brewer.pal
(plotly引用的函数)中,具体写着
All the sequential palettes are available in variations from 3 different values up to 9 different values.
[...]
For qualitative palettes, the lowest number of distinct values available always is 3
由于 build_plotly()
(函数 plotly()
在内部调用)总是调用 brewer.pal()
(参见第 474 行 here),如果不重写 build_plotly()
函数不调用 brewer.pal()
少于 3 个数据 类。
同时,要关闭警告,将绘图输出分配给一个对象并将 print(object)
语句包装在 suppressWarnings()
中,如下所示:
plotly_plot <- plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = Sepal.Length>6, colors = c("#132B43", "#56B1F7"),
mode = "markers")
suppressWarnings(print(plotly_plot))