在 ggplot 中,您可以单独更改每个点的不透明度:
ggplot(iris)+geom_point(aes(x=Sepal.Width, y=Sepal.Length, fill=Petal.Width, color=Petal.Width, alpha=Petal.Length))
。有没有办法在情节上做到这一点?这不起作用:
plot_ly(iris, x=~Sepal.Width, y=~Sepal.Length, color=~Petal.Width, alpha=~Petal.Length)
给出
Error in vapply(traces, function(x) x[["alpha"]] %||% 1, numeric(1)) :
values must be length 1,
but FUN(X[[1]]) result is length 150
第一个问题是不透明度是通过
opacity=
中的 plotly
属性设置的,而不是通过 alpha=
设置。此外,不透明度是 marker
属性的一个属性。最后,您必须手动将值转换为 0 到 1 的范围,我使用 scales::rescale
。另请注意,与 ggplot2
不同,您不会获得不透明度的图例。
library(plotly)
plot_ly(iris,
x = ~Sepal.Width, y = ~Sepal.Length,
color = ~Petal.Width,
marker = list(
opacity = ~ scales::rescale(Petal.Length, to = c(0, 1))
)
)