Reputation: 11
I was trying to make a 2D hexbin map of the US in R and I followed this guide: https://r-graph-gallery.com/328-hexbin-map-of-the-usa.html Context: I was only able to find a couple "how to's" online for what I was trying to do, and the above link is the one I was able to successfully reproduce with my data; the above guide is very similar, if not the same, as the other guides though. Further, judging by screenshots on the others I found, however, it does appear as if they all ended up with the same distorted hexagon issue that I did, so I at least know the solution is not as simple as trying one of those others I found instead, or if it is an issue with a package used in the above guide, it is also presenting as an issue in others that used different packages but similar processes (i.e., https://dges.carleton.ca/CUOSGwiki/index.php/Creating_Hexbin_Maps_in_R)
Anyways, I "successfully" completed the process outlined in that guide, however, some of the hexagons are distorted (AK and HI are most obvious, but not the only ones that are not uniform). You can see this in my final plot, the link/image for which I included at the end, below my code. Now, if you look at the guide I used (as well as others, like I said), you will see that the same issue is occurring in many of the images they post to illustrate the different steps of making this visualization. The only place I don't see the issue is when I navigate to the original source of the geo data (which all the guides I found reference to use for this purpose: https://team.carto.com/u/andrew/tables/andrew.us_states_hexgrid/public/map , however I don't know if that is because the issue isn't with the file, because the view they are showing isn't a still image, or because the image they are showing is actually being pulled from somewhere else entirely.
Now, I know from GIS coursework as a grad student that projection is important in map making, and map distortion can happen when your projection isn't right because you are trying to take something 3D and make it flat, but I'm not certain whether or not that is even related here for a couple reasons (i.e., projection setting isn't visible in this code?)
I can confirm that it does not appear to be a result of resizing the image produced in R. At first, I thought it was because the images had been stretched between plotting them and the creation of the guide (i.e., resizing screenshots), but that does not appear to be the case either. Mine turned out the same (i.e., distorted). And the other reason I thought that the map projection aspect wasn't the issue was because I'm only trying to make 2D hexagons that should all be the same size to create what 'looks like' but does not proportionately represent, the US. [Am I wrong? Is it still a projection issue?]
Can anyone help me figure out how to fix this (or even just help me identify exactly where the issue is coming in, if not a full solution) so I can fix it and make it so each of the hexagons are uniform? Everywhere I see other people referencing using the same shapefile (and similar but varying code to combine it with their data), and in most of their plots, they are also skewed like mine, but it is unclear why it ends up skewed, and no one mentions the distortion anywhere I can find. I just know my visualization won't get approved for what I need it for with that distortion.
Here is my code, that I adapted for my data, from the aforelinked guide. FYI I tried it with both the SHP version of the file and the GEOJSON versions of the geo data, and that didn't make a difference in the distortion aspect.
# Load this file. (Note: I stored in a folder called DATA)
my_sf <- read_sf("us_states_hexgrid.geojson")
# Bit of reformatting
my_sf <- my_sf %>%
mutate(google_name = gsub(" \\(United States\\)", "", google_name))
my_sf <- my_sf %>% rename(State = google_name)
# Show it
plot(st_geometry(my_sf))
ggplot(my_sf) +
geom_sf(fill = "skyblue", color = "white") +
geom_sf_text(aes(label = iso3166_2)) +
theme_void()
# Load MAP data
library(readxl)
state_level_policy_data_for_R_data_viz <- read_excel("state level policy data for R data viz.xlsx")
View(state_level_policy_data_for_R_data_viz)
policy <- state_level_policy_data_for_R_data_viz
remove(state_level_policy_data_for_R_data_viz)
#Perform spatial join
my_sf2 <- my_sf %>%
left_join(. , policy, by=c("State"))
#Check if the join was successful
view(my_sf2)
library(viridis)
my_palette <- rev(magma(8))[c(-1, -8)]
ggplot(my_sf2) +
geom_sf(aes(fill = `Overall Category`), linewidth = 0, alpha = 0.9) +
geom_sf_text(aes(label = iso3166_2), color = "white", size = 3, alpha = 0.6) +
theme_void() +
scale_fill_discrete(breaks=c("High", "Medium", "Fair", "Low", "Negative"))```
Here is my plot: (please also see those in the guide)
Upvotes: 1
Views: 97
Reputation: 11
Thank you to the folks in the comments for providing the answer! I can confirm that
my_sf2 <- st_transform(my_sf2, 3857)
did fix my hexagons. I ran that line and then I just replotted with
ggplot(my_sf2) +
geom_sf(aes(fill = `Overall Category`), linewidth = 0, alpha = 0.9) +
geom_sf_text(aes(label = iso3166_2), color = "white", size = 3, alpha = 0.6) +
theme_void() +
scale_fill_discrete(breaks=c("High", "Medium", "Fair", "Low", "Negative"))```
See below! :)
Upvotes: 0