首页 > 解决方案 > 有没有办法在 R 中绘制 data.frame?

问题描述

我有一个 302 行和 14 列的 data.frame。data.frame 的内容是来自之前 14 次回归分析的系数,我正在寻找一种方法来绘制整个 data.frame,以便系数以红色和蓝色阴影突出显示(分别为负数和正数,0 应该变白)。

行和列名称不应显示在图中,以及实际系数,但我希望能够在某些列和行添加更粗的线。设置了 data.frame,理论上行和列是分组的,因此在这些分组周围添加线条将有助于强调这一点。

我已经尝试过 corrplot 和 ggplot。corrplot(df, is.corr = FALSE) 给了我一些与我想要的东西相关的东西,但情节太长了(由于 302 行)。如果可能,它们(行)应该自动调整它们的高度,以便整个绘图可见。我的主要目标主要是直观地检查颜色的可能模式。

下面是我的数据片段。

                          ingen0 kommune3 kommune8 kommune9 diagnose1 diagnose2 diagnose7 diagnose12 diagose13  psyk5  psyk9 psyk10  krim4  krim6
abdominalomfang            0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.002
adoption1                  0.000    0.000    0.274    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.000
adoptions_anbr1            0.000    0.965    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.585
afsonforfods_mor1          0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000 -0.017
afsonforfodsfarr1          0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.183
agteskab_laengde_far       0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000    -0.001  0.000  0.000  0.000  0.000 -0.008
agteskab_laengde_mor       0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000 -0.002
akutkejsfoed1              0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000 -0.127  0.000  0.000  0.000  0.000
alder_far                 -0.003    0.000    0.009    0.000     0.001     0.000     0.000      0.000     0.000  0.000  0.000  0.002  0.000 -0.001
alder_mor                  0.000    0.000    0.004    0.000     0.000    -0.025     0.000      0.000     0.000  0.004  0.000  0.000 -0.007 -0.012
alm_lage_sysi_far         -0.008    0.000    0.005    0.000     0.001     0.004     0.002      0.006     0.000  0.000  0.467  0.003  0.000  0.003
alm_lage_sysi_mor         -0.007    0.000    0.009    0.003     0.000     0.006     0.003      0.006    -0.002  0.006  0.003  0.005  0.000  0.002
anbringelse1              -2.009    0.005   -1.696   -0.092     0.260     0.217     0.000      0.000     0.000  0.213 -0.092 -0.175 -0.392  0.169
anholdtforfods_far1        0.000    0.000    0.000    0.000     0.000     0.107     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.131
anholdtforfods_mor1        0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000 -0.214
antaldiag_far             -0.006    0.000    0.019    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.051
antaldiag_mor              0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.000
antdage_t_far              0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.000
antdage_t_mor              0.000    0.000    0.000    0.000     0.000     0.000     0.000      0.000     0.000  0.000  0.000  0.000  0.000  0.001
apgarscore_efter5minutter  0.047   -0.091   -0.044    0.000     0.000    -0.027     0.000     -0.010     0.009  0.000  0.000  0.000  0.000  0.005

以下可用于重现 corrplot。我还没有设法在 ggplot 中产生任何成功的东西。

A <- structure(list(ingen0 = c(0, 0, 0, 0, 0, 0, 0, 0, -0.003, 0, 
-0.008, -0.007, -2.009, 0, 0, -0.006, 0, 0, 0, 0.047), kommune3 = c(0, 
0, 0.965, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.005, 0, 0, 0, 0, 0, 0, 
-0.091), kommune8 = c(0, 0.274, 0, 0, 0, 0, 0, 0, 0.009, 0.004, 
0.005, 0.009, -1.696, 0, 0, 0.019, 0, 0, 0, -0.044), kommune9 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.003, -0.092, 0, 0, 0, 0, 0, 0, 
0), diagnose1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0.001, 0, 0.001, 0, 
0.26, 0, 0, 0, 0, 0, 0, 0), diagnose2 = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, -0.025, 0.004, 0.006, 0.217, 0.107, 0, 0, 0, 0, 0, -0.027
), diagnose7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.002, 0.003, 
0, 0, 0, 0, 0, 0, 0, 0), diagnose12 = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0.006, 0.006, 0, 0, 0, 0, 0, 0, 0, -0.01), diagose13 = c(0, 
0, 0, 0, 0, -0.001, 0, 0, 0, 0, 0, -0.002, 0, 0, 0, 0, 0, 0, 
0, 0.009), psyk5 = c(0, 0, 0, 0, 0, 0, 0, -0.127, 0, 0.004, 0, 
0.006, 0.213, 0, 0, 0, 0, 0, 0, 0), psyk9 = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0.467, 0.003, -0.092, 0, 0, 0, 0, 0, 0, 0), psyk10 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0.002, 0, 0.003, 0.005, -0.175, 0, 0, 0, 
0, 0, 0, 0), krim4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, -0.007, 0, 
0, -0.392, 0, 0, 0, 0, 0, 0, 0), krim6 = c(0.002, 0, 0.585, -0.017, 
0.183, -0.008, -0.002, 0, -0.001, -0.012, 0.003, 0.002, 0.169, 
0.131, -0.214, 0.051, 0, 0, 0.001, 0.005)), row.names = c("abdominalomfang", 
"adoption1", "adoptions_anbr1", "afsonforfods_mor1", "afsonforfodsfarr1", 
"agteskab_laengde_far", "agteskab_laengde_mor", "akutkejsfoed1", 
"alder_far", "alder_mor", "alm_lage_sysi_far", "alm_lage_sysi_mor", 
"anbringelse1", "anholdtforfods_far1", "anholdtforfods_mor1", 
"antaldiag_far", "antaldiag_mor", "antdage_t_far", "antdage_t_mor", 
"apgarscore_efter5minutter"), class = "data.frame")

library(corrplot)
corrplot(A, is.corr = FALSE)

上面提到的问题是我的原始 data.frame 中的行数(302) - 它变得人满为患,而且我没有想要添加行 fx 的可能性 - 因此我正在寻找其他选项。

标签: rggplot2plot

解决方案


302 类别有很多显示,特别是如果您想查看类别。(我们通常需要大约 10 页来显示这么多行文本。)

一种方法是制作一个交互式绘图,您可以在其中悬停以查看类别:

首先,一些假数据:

library(tidyverse)
df <- data.frame(
  category = rep(colors(), each = 14),
  col = letters[1:14],
  cor = rnorm(9198)
)

然后绘制为 ggplot 平铺网格:

ggplot(df, aes(col, category, fill = cor)) + 
  geom_tile() +
  scale_fill_gradient2(low = "red", mid = "white", high = "blue")

plotly::ggplotly(.Last.value)

左侧的类别被严重过度绘制并且看起来很垃圾(可能值得添加theme(axis.text.y = element_blank())),但您仍然可以使用 plotly 进行交互探索。

在此处输入图像描述


推荐阅读