r - 使用 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
我用d3.js标记这个- 我不熟悉 - 尽管问题是关于但请随意编辑标签和/或发布。R
. 我希望能吸引熟悉两者的社区成员。
谢谢。
解决方案
需要在布局中进行一些进一步的工作/调查,但这是一种方法。
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 日创建
推荐阅读
- reactjs - 使用setState后如何获取最新状态值?
- javascript - Enabling a button based on selected values
- javascript - 如果在一段时间内发送,则回复消息
- python - 合并 Pandas 中的两列
- java - 如何检测 gson.fromjson() 是否有多余的元素
- css - 如何使用 flexbox 使无序列表响应?
- javascript - 修复“无法读取未定义错误的属性 handleClick”(以及与从 DOM 中删除项目相关的问题)
- c# - 创建和发送 Smtp 附件后渲染时出现 System.Web.HttpException
- node.js - 另一个 Web 进程在启动后 60 秒内未能绑定到 $PORT
- javascript - 当我点击页面底部时,使用 magicscroll 和 greensock 完成的每项更改都会重置