1

My manager asked me to create a bivalent chloropleth map in R from a csv file that contains latitude/longitude data and two variables. I’ve tried to use this tutorial and this stack overflow post but have been unsuccessful – the plot comes up completely empty.

This is an example of what my manager is looking for: https://jech.bmj.com/content/jech/75/6/496/F1.large.jpg

I’ve tried to use this tutorial and this stack overflow post but have been unsuccessful – the plot comes up completely empty.

Below is a mini reproducible version of the data.

df <- data.frame(Region = c(1001, 1003, 1005, 1007),
                 ID = c(5, 6, 7, 8),
                 latitude = c(32.53953, 30.72775, 31.86826, 32.99642),
                 longitude = c(-86.64408, -87.72207, -85.38713, -87.12511),
                 variable_1 = c(0.3465, 0.3028, 0.4168, 0.3866),
                 variable_2 = c(0.44334, 0.45972, 0.46996, 0.44406))  

I am not well-versed in mapping (or in R, frankly) so I would be deeply appreciative of any help this community could provide. Even understanding what additional data I need to create a bivalent plot would be really helpful.

Thank you and please let me know of any additional info I could provide!

Bushidov
  • 713
  • 4
  • 16
shasui1
  • 13
  • 3

1 Answers1

0

Here is how you can achieve such choropleth map. First you need to load/install the necessary packages:

library(biscale)
library(ggplot2)
library(cowplot)
library(sf)
library(dplyr)

Then you need to compute the bi_class between the two variables that will be used to map each group (low,medium,high) for each combination.

df = bi_class(df, x= variable_1, y=variable_2, style="quantile", dim = 3)

As per documention on the package you can change the dim argument to create a 2x2 or 4x4 matrix

Then for what I saw within your data you are looking into counties in Alabama. For this you can look into the tigris package. (not limited to counties)

Al_county <- tigris::counties(state = "Alabama", cb = TRUE) %>% st_as_sf()

Finally, you can merge your data frame into the imported data frame with GEOID and Region. Make sure to add a 0 in from of your 'Region' if it's missing:

GEOID (in imported data) Region (in your df)
01001 1001
01003 1003
01005 1005
01007 1007
df$Region = paste0("0", df$Region) # Add 0 in front of Region values
Al_county = Al_county %>% left_join(df, by= c("GEOID"= "Region")) # Join the 2 data frames

Now the data is ready to be plotted and you can follow the documentation from here

map = ggplot() +
  geom_sf(data = Al_county, aes(fill=bi_class))+
  bi_scale_fill(pal = "GrPink", dim = 3)+
  labs(subtitle = "Var 1 and Var 2 in Alabama") +
  bi_theme()+
  theme(legend.position = "none")

legend <- bi_legend(pal = "GrPink", dim = 3, xlab = "Higher Var 1 ", ylab = "Higher Var 2 ",  size = 8)

finalPlot <- ggdraw() + draw_plot(map, 0, 0, 1, 1) + draw_plot(legend, 0.6, 0.7, 0.4, 0.15)

finalPlot

enter image description here

Bushidov
  • 713
  • 4
  • 16
  • Thank you so so much! I will work through your recommendations! – shasui1 Sep 16 '22 at 14:25
  • Glad I could help you improve your skills! If your question has been answered, please make sure to accept an answer for further references. – Bushidov Sep 22 '22 at 05:02