1. 首页
  2. 其他

Plotly 的填充颜色默认为半透明,想要不透明

本文介绍了Plotly 的填充颜色默认为半透明,想要不透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 plotly 在 R 中构建地图.我将在回复这篇文章时分享构建数据框所需的代码.这是我用来绘制图表的代码:

佛罗里达 %>%group_by(组) %>%plot_ly(x = ~long, y = ~lat,颜色 = ~color_vals, 颜色 = '蓝色') %>%add_polygons(line = list(width = 2),# fillcolor = '这里发生了什么',不透明度 = 1,showlegend = FALSE)

这是我得到的输出:

我的问题很容易理解,但我找不到解决此问题的方法.我希望每个不同县的填充颜色与该县的边界线具有相同的蓝色阴影.在 plotly 的文档中,它声明了填充颜色:

填充颜色(颜色)
设置填充颜色.默认为线条颜色、标记颜色或标记线颜色的半透明变体,以可用者为准.

这就是问题所在……我不想要线条颜色的半透明变体,我想要实际的线条颜色!我尝试将许多不同的值传递给 fillcolor 参数,但没有运气.对此的任何帮助将不胜感激!

谢谢!

编辑 – 佛罗里达数据帧上的 dput 非常大,超过了 RStudio 控制台的最大打印输出.不过,这里有一些类似的、可重复的示例 –

I am trying to build a map in R using plotly. I will share the code required to build the dataframe in a reply to this post. Here is the code I am using to plot the chart:

florida %>%
  group_by(group) %>%
  plot_ly(x = ~long, y = ~lat, 
          color = ~color_vals, colors = 'Blues') %>%
  add_polygons(line = list(width = 2),
               # fillcolor = 'WHAT GOES HERE',
               opacity = 1, showlegend = FALSE)

and here is the output I am getting:

My issue is simple to understand, but I cannot find a way to fix this. I’d like the fillcolor for each of the different counties to be the same shade of blue as the boundary lines for that county. In plotly’s documentation, it states for fillcolor:

fillcolor (color)
Sets the fill color. Defaults to a half-transparent variant of the line color, marker color, or marker line color, whichever is available.

And herein lies the problem… I don’t want a half-transparent variant of the line color, I want the actual line color! I’ve tried passing many different values to the fillcolor parameter, but have had no luck. Any help with this would be super appreciated!

Thanks!

EDIT – the dput on the florida dataframe is very very large, exceeding the RStudio console’s print output max. Some similar, reproducible examples are here though – https://plot.ly/r/county-level-choropleth/.

解决方案

I digged a bit into plotly and did not find a direct solution to this. However, we can use the function prependContent from the htmlwidgets package to tweak the rendered plot.
Here is a simple example:

library(plotly)
library(dplyr)
library(htmlwidgets)
library(htmltools)

# define utility function to adjust fill-opacity using css
fillOpacity <- function(., alpha = 0.5) {
  css <- sprintf("<style> .js-fill { fill-opacity: %s !important; } </style>", alpha)
  prependContent(., HTML(css))
}


ggplot2::map_data("world") %>%
  group_by(group) %>%
  plot_ly(x = ~long, y = ~lat) %>%
  add_polygons() %>%
  fillOpacity(alpha = 1)

We basically inject some CSS into the widget container and use !important in order to make sure the styles wont be overwritten.

Here you can see the results for alpha = 0, 0.25, 0.75 and 1:

这篇关于Plotly 的填充颜色默认为半透明,想要不透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

Plotly 的填充颜色默认为半透明,想要不透明 为WP2原创文章,链接:https://www.wp2.cn/other/plotly-%e7%9a%84%e5%a1%ab%e5%85%85%e9%a2%9c%e8%89%b2%e9%bb%98%e8%ae%a4%e4%b8%ba%e5%8d%8a%e9%80%8f%e6%98%8e%ef%bc%8c%e6%83%b3%e8%a6%81%e4%b8%8d%e9%80%8f%e6%98%8e/