简体   繁体   English

R - 如何绘制一个二维矩阵,其中每个 (i,j) 条目的大小是标记的大小?

[英]R - How can I plot a 2D matrix where the magnitude of each (i,j) entry is the size of the marker?

Using R (version 3.5.1), I am trying to make this plot:使用 R(版本 3.5.1),我试图制作这个图:

感兴趣的情节

Right now, I have a 27x27 matrix without column names.现在,我有一个没有列名的 27x27 矩阵。 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).此矩阵中的条目与 (i,j) 条目的“大小”相关——例如,与 (a,b) 或 (e,f) 相关联的值。 How can I do this in R?我怎样才能在 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.我尝试了balloonplot,但我无法让它工作,因为我的 R 版本太旧了,而且由于某种原因我无法更新我的 Conda 环境。 Is there a function in ggplot2 that would be a good substitute? ggplot2 中是否有一个函数可以作为很好的替代品?

My (27x27) matrix looks something like this:我的 (27x27) 矩阵如下所示:

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).因此,与 (1,1) 相关联的条目将大于 (1,2),但 (1,5) 将是图中最大的正方形(或圆,无关紧要)。 I also have a DataFrame to work with.我还有一个 DataFrame 可以使用。 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.注意:在我的 Jupyter Notebook 的 Conda 环境中,我似乎无法获得大于 3.5.1 的 R 版本。 Because of this, I can't use balloonplot().因此,我不能使用balloonplot()。

Thanks!谢谢!

ggplot2 can do this nicely. ggplot2可以很好地做到这一点。 Let's start with an example matrix and then convert it into a data.frame, since that's what ggplot2 works with.让我们从一个示例矩阵开始,然后将其转换为 data.frame,因为这是ggplot2工作方式。

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.现在我们已将您的矩阵转换为 data.frame,让我们将其重塑为长格式,以便我们可以绘制它。

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.请注意,我将 XCol 和 YCol 列转换为因子,以便它们按照我们希望它们在图中的方式进行排序。

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 图

You can play with the range part of scale_size_continuous to adjust the size to what works best for your purposes.您可以使用scale_size_continuous的 range 部分将大小调整为最适合您的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM