Search code examples
rplotspatialfrequency

R - Plot Spatial Frequency Data as Matrix


I want to do something really easy but cannot figure out how.

What I got:

A dataset of trees (35 species) found in various locations (30) which were in different elevations (--> 30 levels as well). My aim is now to visualize the information of the elevations the trees were found in. However, since most of them were quite scattered, I do have a lot of intraspecific variation.

What I want:

I just want a plot where y is the elevation and x the tree species. Each combination of elevation and species should be represented as a square color-coding the number of tree individuals. This should then result in a panel of squares similar to a discreet heatmap.

What I found:

Since I'm at loss, how you would call such a plot, I did not find anything useful. After searching for spatial (or here and here) and frequency or even presence/absence data, I ended up with a lot of overcomplicated stuff which still won't help me...

Example Data.Frame:

data.frame(elevation = c(103, 260, 307, 505),
 spec1 = c(0, 1, 4, 0),
 spec2 = c(11, 15, 4, 7),
 spec3 = c(3, 1, 5, 5),
 spec4 = c(5, 1, 1, 1))

(I'm sorry the title is really crappy, I just couldn't think of any!)


Solution

  • Do you mean something like this?

    df <- data.frame(elevation = c(103, 260, 307, 505),
     spec1 = c(0, 1, 4, 0),
     spec2 = c(11, 15, 4, 7),
     spec3 = c(3, 1, 5, 5),
     spec4 = c(5, 1, 1, 1))
    
    library(tidyverse);
    df %>%
        gather(species, value, 2:5) %>%
        ggplot(aes(x = species, y = elevation, fill = value)) + geom_tile();
    

    enter image description here