首页 > 解决方案 > ggplot中带有圆圈而不是瓷砖的热图

问题描述

如果我有一个整数向量(或者如果我将它放入矩阵中),是否有使用 ggplot 或其他可以将每个点作为圆而不是平铺的包来绘制热图?

> counts
  [1] 1949 1690 1935 2480 1441 1141 2079 1587  517 1028  535 2180 1692 3916 1784
 [16] 3028 1911 1329 1759 1478 1080 2835 2187 3230 2932 3527 1538 1489 1493 1170
 [31] 3311 4982 4842 4899 1339 3594 2562 1821 1077 1072 4312 3395 3522 1037 3270
 [46] 4813 1686 1955 3290 1288 2592 3717  213 2840 3938 3882 1807 1582 2519 2600
 [61] 4093 2318 3028 3074 1833 3453 2539 2665 2491 2696 2819 1177 1707 1832 3223
 [76] 2366 3742 2648 1594 2112 2239 2840  414 1094 2621 3116 1939 2847 1781 1054
 [91] 1098 1977 1061 2441 2367  723 1126 2550  586  515

这样它看起来像这样(以 x 乘以 y 矩阵排列): 在此处输入图像描述

标签: rggplot2heatmap

解决方案


如果您希望无论缩放如何都可以触摸圆圈,ggforce::geom_circles可能会有所帮助:

z <- runif(100, 0, 5000)  
  
library(dplyr); library(ggplot2)
data.frame(z = z) %>%
  mutate(row = (row_number() - 1) %/% 10 + 1,
         col = (row_number() - 1) %% 10 + 1) %>% 
  ggplot() +
  ggforce::geom_circle(aes(x0 = col, y0 = row, fill = z, r = 0.5)) +
  scale_y_reverse() +
  scale_fill_gradient(low = "yellow", high = "red", breaks = 1000*(0:6)) +
  guides(fill = guide_colorsteps(barwidth = 0.3, barheight = 17)) +
  coord_equal(clip = "off") +
  theme_void()

在此处输入图像描述


推荐阅读