pdil / usmap

🗺 Create US maps including Alaska and Hawaii in R
https://usmap.dev/
GNU General Public License v3.0
74 stars 16 forks source link

Changing label color only if the area is dark #69

Closed wonjeongwon closed 10 months ago

wonjeongwon commented 1 year ago

Is your feature request related to a problem? Please describe. I generated a map with state labels, and used color brewer. Some states have darker fill color, and some states have light fill color. So when I use a black label, it's a bit hard to see the color of the label on the dark states.

Describe the solution you'd like If I could manually assign colors by group, like an aes argument, that would be great. In that way, I can assign label colors for the states that I expect to be in darker or lighter shades.

Describe alternatives you've considered I tried to generate two maps and overlay them together - first only with dark shade states and then with light shade states. But it didn't work out well due to difference in scales.

Additional context Here's an example plot where it's a bit hard to read the labels in the dark orange states. I know we can still read them quite easily, but ideally, I want these labels to be white. example_230330

Thank you in advance!

pdil commented 1 year ago

Sorry I missed this post, I'm not sure of an easy way to do that with the labeling that usmap does automatically but it's possible to manually specify your own text or label in addition to the plot_usmap result, for example:

library(ggplot2)
library(usmap)

state_labels <- usmapdata::centroid_labels("states")

plot_usmap(data = statepop, values = "pop_2015") + 
  geom_label(data = state_labels, aes(x = x, y = y, label = abbr)) +
  scale_fill_continuous(low = "white", high = "red", guide = "none") +
  scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))

The key line is geom_label(data = state_labels, aes(x = x, y = y, label = abbr)). This creates a label instead of a text and the label has a border with a background which should help legibility:

image