0

I want to make a choropleth map in RStudio using a geoJSON file that I read using the read_sf function and a dataframe for the heatmap and the labels. So far this is the code that I came up with:

indonesia_geojson <- read_sf("path/to/indonesia.geojson")
indonesia_geojson <- merge(indonesia_geojson, heatmap, by.x = "state", by.y = "PROVINCE_INA")
indonesia_heatmap_final <- ggplot(indonesia_geojson) + geom_sf(data = indonesia_geojson, linewidth = 0.05, aes(fill = heatmap$Frequency_total)) +
  scale_fill_gradientn("Number of sequences", 
                       colours = c("white","#858585","black"), 
                       values = rescale(c(0,1000,20000)),
                       guide = "colorbar", limits=c(0,20000)) +
  geom_sf_text(aes(label = heatmap$Frequency_total), colour = "black", size=8, family="OpenSans_bold") +
  geom_sf_text(aes(label = heatmap$PROVINCE_INA), colour = "black", size=4, family="OpenSans_light", vjust = 2.4) +
  geom_sf_text(aes(label = paste0("◉ ", heatmap$Frequency_Alpha)), colour = "dark red", size=4, family="NotoSansPro_medium", vjust = 4.0) +
  geom_sf_text(aes(label = paste0("◉ ", heatmap$Frequency_Beta)), colour = "orange", size=4, family="NotoSansPro_medium", vjust = 5.4) +
  geom_sf_text(aes(label = paste0("◉ ", heatmap$Frequency_Delta)), colour = "dark green", size=4, family="NotoSansPro_medium", vjust = 6.8) +
  geom_sf_text(aes(label = paste0("◉ ", heatmap$Frequency_Omicron)), colour = "blue", size=4, family="NotoSansPro_medium", vjust = 8.2) +
  geom_sf_text(aes(label = paste0("◉ ", heatmap$Frequency_XBB.1)), colour = "dark blue", size=4, family="NotoSansPro_medium", vjust = 9.6) +
  theme_void() +
  ggtitle("Peta Whole Genome Sequences dan VOC per Provinsi") +
  theme(plot.subtitle = element_text(size = 10, color = 'black')) +
  theme(plot.title = element_text(family = "OpenSans_bold", hjust = 0.5, size = 28), legend.position = c(.9,.85)) +
  guides(fill = guide_colorbar(barwidth = 0.3, barheight = 2, position = "left", 
                               title.position = "top"
  ))
indonesia_heatmap_final
ggsave("path/to/example.png", indonesia_heatmap_final, width = 1920, height = 1080, units = "px", bg = "white")

This is the output image that I get from this code, but some of these numbers are actually up to 5 digits long and so will overlap a lot. I need to nudge these labels but as a group. I tried using multiple lines of geom_text_repel but ggrepel did not move them as a group. (Makes sense, since I did not specify that they belong in the same group, which in this case means they belong in the same province).

I also tried making an additional column for my label in my original dataframe (the object named heatmap here) and formatted them using a HTML-styled tagging system like below:

heatmap$label <- paste0(
  "<p style='color:black; font-size: 8pt; font-family: OpenSans_bold'>", heatmap$Frequency_total, 
  "</p>\n<p style='color:black; font-size: 4pt; font-family: OpenSans_light'>", heatmap$PROVINCE_INA, "</p>", 
  "</p>\n<p style='color:#8b0000; font-size: 4pt; font-family: NotoSansPro_medium'>", heatmap$Frequency_Alpha, "</p>", 
  "</p>\n<p style='color:#ffa500; font-size: 4pt; font-family: NotoSansPro_medium'>", heatmap$Frequency_Beta, "</p>", 
  "</p>\n<p style='color:#006400; font-size: 4pt; font-family: NotoSansPro_medium'>", heatmap$Frequency_Delta, "</p>", 
  "</p>\n<p style='color:#0000ff; font-size: 4pt; font-family: NotoSansPro_medium'>", heatmap$Frequency_Omicron, "</p>", 
  "</p>\n<p style='color:#00008b; font-size: 4pt; font-family: NotoSansPro_medium'>", heatmap$Frequency_XBB.1, "</p>")

but RStudio did not even plot my labels. Can anyone help? Thank you in advance.

0 Answers0