usmap icon indicating copy to clipboard operation
usmap copied to clipboard

Unable to add usmap$layer to ggplot object

Open SvbZ3r0 opened this issue 4 years ago • 7 comments

Describe the bug As I understand it, currently the only way to have county data and state data in the same map is to create the maps separately, extract their layers and add it to a single ggplot object. However, this method throws an error.

Simplest code to reproduce

ggplot() +
  plot_usmap()$layers[[1]]

Error

Error in FUN(X[[i]], ...) : object 'x' not found

Environment:

  • Windows 10 x64
  • R v4.0.3
  • ggplot2_3.3.2
  • usmap_0.5.1

Additional info This method is also the only way to add multiple levels of data on the same map. So this should be looked into regardless of #26

SvbZ3r0 avatar Nov 09 '20 05:11 SvbZ3r0

@SvbZ3r0 thanks for finding this, it seems like it broke when I fixed the ggplot warnings (https://github.com/pdil/usmap/commit/b493f091e9f09cb6ad341c166bf66a6176042400). This wasn't really an officially supported method so it's probably better to build in this functionality properly (issue #26) so I will go ahead and prioritize that feature.

pdil avatar Nov 09 '20 16:11 pdil

Since this method isn't officially supported, is there a different way to add multiple layers of data within the same map? For example, a map with county level data masked with another map of county level data. I understand the functionality currently proposed in #26 only supports one layer of state level data and one layer of county level data. Would it be possible to fully support all the possibilities that arose using the unofficial method?

SvbZ3r0 avatar Nov 09 '20 17:11 SvbZ3r0

@SvbZ3r0 Originally the thought was to allow for something like this:

IMG_2348

where you could show the entire state but then selectively show counties within them. Since the only way usmap can show data is by filling in states or counties (unless you manually add the data points as in the earthquake example) I'm not sure how you would add multiple layers of data within the same map. Do you have an example of what you want to be able to do?

pdil avatar Nov 09 '20 20:11 pdil

I didn't want to link to my own blog, but here goes: CodeReido - R Crowds Inherently Blue. This is what I was trying to do, and what led me here. Tl;dr: I want to correlate voting patterns to population density, by showing which area voted for which party and using an alpha mask on top of that. The idea is that the mask would be more transparent in high density areas, thereby giving more visual prominence to high density areas while still retaining visual data as to how the rest of the country voted.

SvbZ3r0 avatar Nov 09 '20 21:11 SvbZ3r0

I didn't want to link to my own blog, but here goes: CodeReido - R Crowds Inherently Blue. This is what I was trying to do, and what led me here. Tl;dr: I want to correlate voting patterns to population density, by showing which area voted for which party and using an alpha mask on top of that. The idea is that the mask would be more transparent in high density areas, thereby giving more visual prominence to high density areas while still retaining visual data as to how the rest of the country voted.

@SvbZ3r0 that was a nice read! So your goal would be to have a county map where counties are filled red/blue (according to party voted) and then the alpha of each county mapped to the population density. I'm not sure if that would be possible the way plot_usmap is set up currently. Did you used to be able to do that using the workaround? If so can you provide sample code?

pdil avatar Nov 11 '20 17:11 pdil

Thank you! That is my goal. I came across the workaround on this StackOverflow question, and since what was being tried there is similar to what I want to do, I assumed the old version would have supported my use case. Keeping in mind that I'm extremely new to R, this assumption might very well be wrong. I'll try it with the old version when I have the time, and I'll let you know.

SvbZ3r0 avatar Nov 11 '20 17:11 SvbZ3r0

@SvbZ3r0 thanks, that StackOverflow helps a lot. I'll keep an eye on that while working to incorporate #26 properly in the meantime.

pdil avatar Nov 11 '20 20:11 pdil

See resolution in #26

pdil avatar Jan 20 '24 04:01 pdil