简体   繁体   中英

Plotly choropleth map opacity

I'm following the model for building a choropleth map given on this page: https://plot.ly/r/county-level-choropleth/ . The polygon colors are somewhat transparent, and nothing I've tried to change the opacity has had any effect.

library(ggplot2) # For the demo map data
library(plotly)

ohio_counties <- map_data('county', 'ohio')
# This id simulates the categorical variable I'm trying to represent with color
ohio_counties$id <- letters[ohio_counties$group %% 6 + 1]

ohio_counties %>%
  group_by(group) %>%
  plot_ly(x = ~long,
          y = ~lat,
          color = ~id) %>%
  add_polygons(line = list(width = 0.4)) 

I've tried adding opacity = 1 as a parameter to the plot_ly() call and in add_polygons , and I've tried using my own custom color scales, but all appear to have the same amount of transparency. I can get rid of the grid lines, but then I'm still stuck with faded pastel colors.

在此处输入图片说明

EDIT: Compare a map made in Plotly versus another I made in Leaflet using the exact same palette, just to the show the difference between what I am getting and what I would like. (Ignore the background leaflet map and county lines, has no effect on the colors.)

Plotly

绘制地图

Leaflet

传单制作地图

It appears that your opacity is already maxed out for the default color palette... As you see, I can make the image even less opaque:

> ohio_counties %>%
+     group_by(group) %>%
+     plot_ly(x = ~long,
+             y = ~lat,
+             color = ~id,opacity=.2) %>%
+     add_polygons(line = list(width = 0.4)) 

极度透明

So, if you use a different color palette (in this case I use viridis::magma)

> require(viridis)
> ohio_counties %>%
+     group_by(group) %>%
+     plot_ly(x = ~long,
+             y = ~lat,
+             color = ~id,colors=viridis::magma(6)) %>%
+     add_polygons(line = list(width = 0.4))

在此处输入图片说明

This drove me to distraction. As much as Plotly is getting very powerful, it still has some very basic shortcomings. I was trying to make a choropleth map of Chinese provinces with imported polygon data, but couldn't figure out why the colours were washed out. I also had the problem that adding another trace on top to get black contours would colour the whole map light grey, covering up the colours underneath.

After spending more time than I ought have to figure it out, I finally discovered this in the manual, under scattergeo (because the type of this map is actually not choropleth):

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.

In other words: color and colors actually work on the line color, not fillcolor . When fillcolor is not set, it acquires the same color as the line, at 50% opacity. There should be a way for color and colors to work directly on fillcolor , but it appears that's not the case. Fillcolor can only be set to a single colour, not to change its value depending on a variable.

I'm not an expert on Plotly, but this is my understanding of how it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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