rexcelggplot2

Replicate Excel contour map in R- Location and Counts


I have data that tells me how often individuals are visiting a location. Locations are transect data (so a number and a letter), and I have count data for how often that location appears, depending on what variable I select. This example shows where a food tree was used (left) and overall number of visits (right).

example from excel

I had the data in Excel and very easily made a contour map, but excel doesn't have a lot of controls and I'd much rather just use GGplot or something else in R. I've tried using geom_tile and it's not really what I'm looking for. I want to be able to have multiple variables so I can layer them. All my searches keep returning results based on real maps, which is not really helpful.

Does anyone know of a way to get this kind of map in R?

My data is below, but any matrix will generally work for this. table in excel


Solution

  • The secret is to pivot the matrix into long format, convert your lettered column names into numbers, then draw using geom_contour. The counts in each cell need to be mapped to the z aesthetic.

    library(tidyverse)
    
    df %>%
      rownames_to_column() %>%
      pivot_longer(-rowname, names_to = "colname") %>%
      mutate(xval = as.numeric(factor(colname)),
             yval = as.numeric(rowname)) %>%
      ggplot(aes(xval, yval)) +
      geom_contour_filled(aes(z = value, colour = after_stat(level)),
                   fill = NA, binwidth = 100) +
      scale_y_reverse(breaks = 15:6) +
      scale_x_continuous(breaks = seq(length(df)),
                         labels = colnames(df),
                         limits = c(1, length(df)),
                         position = "top") +
      scale_color_manual(values = alpha("darkgreen", seq(0.25, 1, 0.25)),
                         labels = paste(0:3 * 100, 1:4 * 100, sep = " - ")) +
      labs(x = NULL, y = NULL, color = NULL) +
      coord_cartesian(expand = FALSE) +
      theme_bw(16) +
      theme(legend.position = "bottom",
            panel.grid.minor.x = element_blank(),
            panel.grid.minor.y = element_blank())
    
    

    enter image description here

    Data used - transcribed from picture in question

    df <- read.table(text = "      E   F   G    H    I    J    K
                              6    0   0    0    1    2    0   0
                              7    0   2    4    9   34   32   7
                              8    0   0   18  112  114  111  24
                              9    0   3  142  159  211  161   0
                              10   0  36  174  311  277  124   1
                              11   0  55  306  155  216   78   4
                              12   0  25  281  393  367  266   9
                              13  10  62  337  235  248  124  57
                              14   5  33  127  127   60   27   3
                              15   0   3    8    0    0    0   0")