带有“plot_ly”的线条和标记的自定义颜色问题
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
假设我想为线条+标记绘图使用自定义颜色。
使用 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