0

Using R (version 3.5.1), I am trying to make this plot:

Plot of interest

Right now, I have a 27x27 matrix without column names. The entries in this matrix correlate to the "magnitude" of the (i,j) entry — for example, the value associated with (a,b) or (e,f). How can I do this in R? I tried balloonplot, but I can't get it to work because my version of R is too old, and I can't update my Conda environment for some reason. Is there a function in ggplot2 that would be a good substitute?

My (27x27) matrix looks something like this:

100, 53, 76, 23, 5330, ... , 90
...
230, 89, 23, 22, 1000, ... , 19

So the entry associated with (1,1) would be bigger than (1,2), but (1,5) would be the largest square in the plot (or circle, it doesn't matter). I also have a DataFrame to work with. It has all of the letters and "space" as the row/column names.

Of note: I can't seem to get a version of R greater than 3.5.1 in my Jupyter Notebook's Conda environment. Because of this, I can't use balloonplot().

Thanks!

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Is there a reason you are stuck using an older version of R? – MrFlick Oct 20 '20 at 02:24
  • try starting with something like `plot(row(m),col(m),pch=15,cex=f(m))` where `f(m)` is an appropriate scaling of the values (this will make black squares on a white background ... – Ben Bolker Oct 20 '20 at 02:51

1 Answers1

2

ggplot2 can do this nicely. Let's start with an example matrix and then convert it into a data.frame, since that's what ggplot2 works with.

library(tidyverse)
MyMat <- matrix(data = rnorm(27*27, mean = 100, sd = 20), 
                ncol = 27, nrow = 27)
MyDataFrame <- MyMat %>% as.data.frame()
names(MyDataFrame) <- c(letters[1:26], "-")

Now that we've got your matrix converted into a data.frame, let's reshape it into long format so that we can plot it.

MyDataFrame <- MyDataFrame %>% 
      mutate(YCol = c(letters[1:26], "-")) %>% 
      pivot_longer(names_to = "XCol", values_to = "Value", -YCol) %>% 
      mutate(YCol = factor(YCol, levels = c("-", letters[26:1])), 
             XCol = factor(XCol, levels = c(letters[1:26], "-")))

Note that I converted the columns XCol and YCol into factors so that they'll sort the way we want them to in the plot.

ggplot(MyDataFrame, aes(x = XCol, y = YCol, size = Value)) +
      geom_point(shape = 15, color = "white") +
      scale_size_continuous(range = c(0.1, 5)) +
      xlab(NULL) + ylab(NULL) +
      theme(panel.background = element_rect(fill="black", color=NA),
            panel.grid.minor.y = element_line(color = NA),
            panel.grid.minor.x = element_line(color = NA),
            panel.grid.major = element_line(colour = NA), 
            legend.position = "none")

ggplot2 graph

You can play with the range part of scale_size_continuous to adjust the size to what works best for your purposes.

shirewoman2
  • 1,842
  • 4
  • 19
  • 31