【发布时间】:2020-08-19 04:03:37
【问题描述】:
在 R 和情节中。我如何尝试使用 alpha 来控制分组散点图的透明度。 我按照Plotly's fillcolor defaults to half-transparency, want no-transparency中的例子:
df <- data.frame(x = rep(LETTERS[1:5], 3),
y = rexp(15, rate = 0.5),
z = c(rep("Adam", 5), rep("Arthur", 5), rep("Ford", 5)))
df <- arrange(df, desc(z))
plot_ly(df, x = ~x, y = ~y, color = ~z, type = 'scatter', stackgroup = 'one',
groupnorm = 'percent', colors = gg_color(Nv), alpha = 1, alpha_stroke = 1)
Plotly's fillcolor defaults to half-transparency, want no-transparency 中给出的解决方案有效:
library(htmlwidgets)
library(htmltools)
fillOpacity = function(., alpha = 0.5) {
css = sprintf("<style> .js-fill { fill-opacity: %s !important; } </style>", alpha)
prependContent(., HTML(css))
}
plot_ly(df, x = ~x, y = ~y, color = ~z, type = 'scatter', stackgroup = 'one',
groupnorm = 'percent', colors = gg_color(Nv), alpha = 1, alpha_stroke = 1) %>%
fillOpacity(alpha = 1)
作品:
但是,在 Shiny 中,这会给出以下警告并且不透明度会丢失
Warning in renderWidget(instance) :
Ignoring prepended content; prependContent can't be used in a Shiny render call
有没有办法解决这个问题?
注意:第二个图是windows中的一个片段,因为当点击下载png plotly按钮时,第二个图仍然将图下载为透明的。这两个问题可能是相关的。
【问题讨论】:
-
plot_ly 中有一个“不透明”参数。你试过用吗?
-
你的意思是阿尔法?我不认为存在不透明的论点。无论如何我都试过了,它没有做任何事情。顺便说一句,我想要散射填充的不透明度。不是酒吧。也许这是添加到布局中的一个选项?