简体   繁体   English

plotly 在 R shiny 中填充颜色透明度

[英]plotly fill color transparency in R shiny

In R and plotly.在 R 和 plotly 中。 How can I tried to use alpha to control the transparency of a grouped scatter plot.如何尝试使用 alpha 来控制分组散点图 plot 的透明度。 I followed the example in Plotly's fillcolor defaults to half-transparency, want no-transparency :我按照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)

But the fill colours are transparent and alpha does not seem to do anything.但是填充颜色是透明的,并且alpha似乎没有任何作用。 在此处输入图像描述

The solution given in Plotly's fillcolor defaults to half-transparency, want no-transparency works: 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)

Works:作品:

在此处输入图像描述

However, in Shiny this gives the following warning and the opacity is lost但是,在 Shiny 中,这会给出以下警告并且不透明度会丢失

Warning in renderWidget(instance) :
  Ignoring prepended content; prependContent can't be used in a Shiny render call

Is there a way to fix this?有没有办法来解决这个问题?

Note: The second figure is a snip in windows, because when clicking on the download png plotly button, the second graph still downloads the graph as transparent.注意:第二个图是 windows 中的一个片段,因为当点击下载 png plotly 按钮时,第二个图仍然以透明的方式下载该图。 The two issues are probably related.这两个问题可能是相关的。

I looked here https://plotly.com/python/filled-area-plots/ and used fill='tonexty' .我在这里查看https://plotly.com/python/filled-area-plots/并使用了fill='tonexty' Now the alpha = 1 works and controls the transparency of the fill.现在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

gives:给出:

在此处输入图像描述

I still think this is an oversight in the API.我仍然认为这是 API 中的疏忽。 when stackgroup = 'one', groupnorm = 'percent' the fill is automatically fill='tonexty' .stackgroup = 'one', groupnorm = 'percent'自动fill='tonexty' there shouldn't be a need to add it in order to manipulate the opacity.不需要添加它来控制不透明度。 Also, alpha = 1 becoming active only because works fill='tonexty' has been added is confusing.此外, alpha = 1仅因为添加了作品fill='tonexty'而变得活跃令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM