[英]plotly fill color transparency in R shiny
在 R 和 plotly 中。 如何嘗試使用 alpha 來控制分組散點圖 plot 的透明度。 我按照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 的 fillcolor 中給出的解決方案默認為半透明,想要不透明的作品:
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 按鈕時,第二個圖仍然以透明的方式下載該圖。 這兩個問題可能是相關的。
我在這里查看https://plotly.com/python/filled-area-plots/並使用了fill='tonexty'
。 現在alpha = 1
起作用並控制填充的透明度。
ply = plot_ly(df, x = ~x, y = ~y, color = ~z, type = 'scatter', stackgroup = 'one',
groupnorm = 'percent', alpha = 1, alpha_stroke = 1, mode = 'marker', opacity=1, fill='tonexty');ply
給出:
我仍然認為這是 API 中的疏忽。 當stackgroup = 'one', groupnorm = 'percent'
自動fill='tonexty'
。 不需要添加它來控制不透明度。 此外, alpha = 1
僅因為添加了作品fill='tonexty'
而變得活躍令人困惑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.