Search code examples
ramcharts

Honeycomb tile map in R


How can I implement this honeycomb tile map in R using the RAmCharts package (or another package that looks as good)?

https://www.amcharts.com/demos/honeycomb-tile-map/


Solution

  • It's easy enough to reproduce the figure in R

    library(tidyverse)
    
    p <- states %>%
      rowwise() %>%
      reframe(x = c(-1, 0, 1, 1, 0, -1, -1) * 0.5 + x,
              y = c(0.25, 0.5, 0.25, -0.25, -0.5, -0.25, 0.25) * 2/sqrt(3) + y, 
              short, name, value) %>%
      ggplot(aes(x, y, fill = value, group = name)) +
      geom_polygon(linewidth = 2, color = "white") +
      geom_text(aes(label = short), data = states) +
      scale_fill_gradient(low = "#fffb77", high = "red", guide = "none") +
      coord_equal() +
      theme_void()
    
    p
    

    enter image description here

    You can easily turn this into an interactive plot using plotly::ggplotly(p).

    The hard part is getting the data. I took this from the JavaScript object on the linked page and transformed it appropriately for use in R:

    states <- structure(list(short = c("AL", "AK", "AZ", "AR", "CA", "CO", 
    "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", 
    "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", 
    "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", 
    "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", 
    "WY"), name = c("Alabama", "Alaska", "Arizona", "Arkansas", "California", 
    "Colorado", "Connecticut", "Delaware", "District of Columbia", 
    "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", 
    "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", 
    "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", 
    "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", 
    "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", 
    "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", 
    "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", 
    "Washington", "West Virginia", "Wisconsin", "Wyoming"), y = c(-5.19615242270663, 
    0, -4.33012701892219, -4.33012701892219, -4.33012701892219, -3.46410161513775, 
    -2.59807621135332, -3.46410161513775, -3.46410161513775, -6.92820323027551, 
    -6.06217782649107, -6.92820323027551, -2.59807621135332, -2.59807621135332, 
    -2.59807621135332, -2.59807621135332, -4.33012701892219, -3.46410161513775, 
    -5.19615242270663, 0, -3.46410161513775, -1.73205080756888, -1.73205080756888, 
    -1.73205080756888, -5.19615242270663, -3.46410161513775, -1.73205080756888, 
    -3.46410161513775, -3.46410161513775, -0.866025403784439, -2.59807621135332, 
    -5.19615242270663, -1.73205080756888, -4.33012701892219, -1.73205080756888, 
    -2.59807621135332, -5.19615242270663, -3.46410161513775, -2.59807621135332, 
    -1.73205080756888, -5.19615242270663, -2.59807621135332, -4.33012701892219, 
    -6.06217782649107, -4.33012701892219, -0.866025403784439, -4.33012701892219, 
    -1.73205080756888, -3.46410161513775, -1.73205080756888, -2.59807621135332
    ), x = c(7.5, 0.5, 3, 6, 2, 3.5, 11, 9.5, 10.5, 8.5, 8, 0.5, 
    2, 6, 7, 5, 5, 6.5, 5.5, 11.5, 8.5, 10.5, 7.5, 4.5, 6.5, 5.5, 
    2.5, 4.5, 2.5, 11, 10, 3.5, 9.5, 9, 3.5, 8, 4.5, 1.5, 9, 11.5, 
    8.5, 4, 7, 4, 4, 10, 8, 1.5, 7.5, 5.5, 3), value = c(4849300L, 
    737700L, 6745400L, 2994000L, 39250000L, 5540500L, 3596600L, 935600L, 
    7288000L, 20612400L, 10310300L, 1419500L, 1634400L, 12801500L, 
    6596800L, 3107100L, 2904000L, 4413400L, 4649600L, 1330000L, 6016400L, 
    6811700L, 9928300L, 5519900L, 2984900L, 6093000L, 1023500L, 1881500L, 
    2839000L, 1326800L, 8944400L, 2085500L, 19745200L, 10146700L, 
    739400L, 11614370L, 3878000L, 3970200L, 12784200L, 1055100L, 
    4832400L, 853100L, 6651100L, 27862500L, 2942900L, 626010L, 8411800L, 
    7288000L, 1850320L, 5778700L, 584150L)), class = "data.frame", row.names = c(NA, 
    51L))