带 R 的圆形气泡图

Zet*_*ina 5 geometry drawing bubble-chart r data-visualization

我想在 R 中重现以下情节:

气泡图

我现在使用packcircles包,并且能够创建图中的绘图:

包圈图

使用以下代码:

require(packcircles)
res <- circleLayout(xyr, limits, limits, maxiter = 1000)
dat <- circlePlotData(res$layout)

doPlot <- function(dat, title)
ggplot(dat) + 
geom_polygon(aes(x, y, group=id), colour="tomato", fill="tomato", alpha=0.3) +
coord_equal(xlim=limits, ylim=limits) +
theme_bw() +
theme(axis.text=element_blank(),
    axis.ticks=element_blank(),
    axis.title=element_blank()) +
labs(title=title)

doPlot(dat, "")
Run Code Online (Sandbox Code Playgroud)

其中xyr是以下形式的数据框:

          x           y    r
1  2.897344 -10.4161236 0.57
2  1.932411  13.0631120 0.52
3  6.839500 -11.0507209 0.52
4 11.117047 -17.6440597 0.77
5 22.395529  -0.6191146 0.45
6 20.313309 -13.3292887 0.69
Run Code Online (Sandbox Code Playgroud)

以及表单的输出数据:

         x         y id
1 10.72973 -14.18673  1
2 10.71182 -14.04498  1
3 10.65923 -13.91213  1
4 10.57524 -13.79654  1
5 10.46515 -13.70546  1
6 10.33587 -13.64463  1
Run Code Online (Sandbox Code Playgroud)

这是一个好的开始,但我需要根据某个变量对圆圈进行标记和着色。显然, dat中的行与输入数据帧中的行的顺序不同。因此,我注意到能够向dat添加任何其他信息(例如,新变量)以用于在ggplot2中按颜色对圆圈进行分组,因为我丢失了对原始数据的任何引用。

按照@rawr的建议,我使用bubbles包通过以下代码重现该图:

require(bubbles)
text = as.character(seq(from = 1, to = 476))
bubbles(value = count,
        color = rainbow(length(count), alpha=NULL)[sample(length(count))],
        label = text,
        width=1600, height=1600)
Run Code Online (Sandbox Code Playgroud)

其中count是 [0,1] 中的数值向量。输出如下:

气泡图

该库允许为每个圆圈提供不同的颜色,但不可能按组为圆圈着色。此外,它允许以非常简单的方式设置标签,这与packcircles不同。无论如何,我无法直接从 R 导出绘图,但我必须将其作为 R Markdown 文档的一部分输出。