首页 > 解决方案 > 使用 ggplot2 创建类似于 d3.js 强制布局的气泡图

问题描述

是否可以使用R最好的方法制作与此类似的气泡图ggplot2

在此处输入图像描述

鉴于此示例中存在三个类别,因此属性为

资料来源:d3indepth.com/force-layout

数据(虽然我真的很确定这种图的数据应该是什么样子)

set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
                  bubble = rep(1:10, 3),
                  radius = round(runif(30, min = 0.5, max = 3), 2),
                  stringsAsFactors = FALSE)
dat

我用标记这个- 我不熟悉 - 尽管问题是关于R. 我希望能吸引熟悉两者的社区成员。但请随意编辑标签和/或发布。

谢谢。

标签: rggplot2data-visualizationcircle-pack

解决方案


需要在布局中进行一些进一步的工作/调查,但这是一种方法。

library(packcircles)
library(tidyverse)

set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
                  id = 1:30,
                  radius = round(runif(30, min = 0.5, max = 3), 2),
                  stringsAsFactors = FALSE)

#Create layouts for each group by splitting, mapping and recombining
dat.gg <- dat %>% 
  split(.$category) %>% 
  map(~circleProgressiveLayout(.x$radius, sizetype='radius')) %>% 
  imap_dfr(~circleLayoutVertices(.x, npoints=50) %>% mutate(category = .y))

#Do the thing
ggplot() + 
  geom_polygon(data = dat.gg, aes(x, y, group = id, fill = category), colour = "black", alpha = 0.6) +
  facet_wrap(~category) +
  scale_fill_viridis_d() +
  theme_void() + 
  theme(legend.position="none", plot.margin=unit(c(0,0,0,0),"cm") ) + 
  coord_equal()

reprex 包(v0.2.1)于 2018 年 11 月 20 日创建


推荐阅读