![](/img/trans.png)
[英]how to correctly set the colorscale in a plot_ly plot with custom colors
[英]Problem with custom colors for both lines and markers with `plot_ly`
让我们假设我想为线条+标记绘图使用自定义颜色。 使用plot_ly()
的colors
和color
参数非常简单,但是一旦我想修改标记本身(使用marker
参数),我就会遇到困难,并且没有在网上找到解决这个特定问题的帮助。 有人能告诉我我做错了什么吗?
# Underlying data
tmp <- mpg %>%
group_by(class,manufacturer) %>%
summarise(models=n())
# Works as expected
tmp %>%
plot_ly(x=~manufacturer,
y=~models,
group=~class,
type="scatter",
color=~class,
colors = scales::hue_pal()(length(n_distinct(tmp$class))), #ggplot colors
mode="lines+markers")
# Issue with markers > idea behind is to have a white center and a marker line with the same color as the line itself
tmp %>%
plot_ly(x=~manufacturer,
y=~models,
group=~class,
type="scatter",
color=~class,
colors = scales::hue_pal()(n_distinct(tmp$class)),
marker = list(color = 'rgba(255, 255, 255, 1)',
line = list(color = scales::hue_pal()(n_distinct(tmp$class)))),
mode="lines+markers")
这可能看起来有点奇怪,但是在您的情况下,精美简单的解决方案是删除:
line = list(color = scales::hue_pal()(n_distinct(tmp$class))))
并且只包括:
line = list(width = 3)
这是你现在的情节:
# Underlying data
tmp <- mpg %>%
group_by(class,manufacturer) %>%
summarise(models=n())
# Issue with markers > idea behind is to have a white center and a marker line with the same color as the line itself
tmp %>%
plot_ly(x=~manufacturer,
y=~models,
group=~class,
type="scatter",
color=~class,
colors = scales::hue_pal()(n_distinct(tmp$class)),
marker = list(color = 'rgba(255, 255, 255, 1)',
line = list(width = 3)),
mode="lines+markers")
tmp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.