气泡图,唯一的度量是大小和颜色。

huangapple go评论61阅读模式
英文:

Bubble chart where the only metric is size and colour

问题

我想制作一个像下面附图中的气泡图一样的图表。

气泡图,唯一的度量是大小和颜色。

1: https://i.stack.imgur.com/e6xHH.png

想法是创建一个 ggplot 风格的气泡图,我只想控制图表的大小和颜色,对位置没有太多控制。气泡应该只是聚集在一起,彼此之间有一个指定的半径,以确保它们不会相互接触。

是否有特定的 ggplot 函数或 R 包可以为我完成这个任务?我确信可以使用 geom_point() 制作一个,但人们应该能够想象有更简单的方法,我宁愿不在这个时候做三角函数计算。

有什么建议吗?

另一个解决方案可能是使用物理效应。让所有的气泡相互吸引,就像受到“重力”作用一样,然后当它们靠得太近时停止吸引。这可能意味着更少的数学计算。即使有一个相关的包,也很高兴看到某个更擅长这种工作的人快速编写的版本。

输入应该类似于以下内容:

birds <- c("Eagle", "Owl", "Falcon", "Ostrich", "Blue Jay", "Chaffinch")
values <- c(43, 23, 54, 112, 16, 12)

df <- data.frame(birds, values)

编辑:我确信这将被标记为重复问题,但我已经竭尽全力在线上和 Stack 上寻找我需要的信息,但没有找到。只有当你确定时,请将其标记为重复问题。如果你不确定,请让其他人来回答。

英文:

I want to make a bubble chart like the one attached below.

气泡图,唯一的度量是大小和颜色。

The idea is to have a ggplot style bubble chart where all I really want to control is the size and colour of the chart, with no real control over the position. The bubbles should just be clustered around each other with a specified radius that should keep them from touching.

Is there a specific ggplot function or R package that can do this for me? I'm sure it's possible to make one with geom_point() but one has to imagine that there is an easier way and I'd rather not do trig at this time of day.

Any pointers?

Another solution might be to use physics. Have all the bubbles attracted to each other by "gravity" and then make them stop when they get too close to each other. That might mean less maths involved. Even if there is a package for it, a quickly coded up version of that by someone more adept at the type of work than me would be nice to see.

Input should be something like this:

birds &lt;- c(&quot;Eagle&quot;, &quot;Owl&quot;, &quot;Falcon&quot;, &quot;Ostrich&quot;, &quot;Blue Jay&quot;, &quot;Chaffinch&quot;)
values &lt;- c(43, 23, 54, 112, 16, 12)

df &lt;- data.frame(birds, values)

Edit: I am certain this is going to be labelled as a repeat question but I have done my jolly hardest to find what I'm looking for online and on stack and I haven't found it. Please only mark as repetitive if you are SURE that it is. If you're not sure, just let someone else answer it.

答案1

得分: 2

使用注释中由@JonSpring提到的圆装填库,从文档中修改的代码

df <- data.frame(birds = c("Eagle", "Owl", "Falcon", "Ostrich", "Blue Jay", "Chaffinch"),
                 values = c(43, 23, 54, 112, 16, 12))

library(ggplot2)
install.packages("packcircles")
library(packcircles)

df$packing <- circleProgressiveLayout(df$values, sizetype='area')

df.gg <- circleLayoutVertices(df$packing, npoints=50)

ggplot() + 
  geom_polygon(data = df.gg, aes(x, y, group = id, fill=id), alpha = 0.6)+
  scale_fill_viridis_c()+
  geom_text(data = df, aes(x=packing$x, y=packing$y, label = birds), size=5, color="black") +
  theme_void() + 
  theme(legend.position="none", plot.margin=unit(c(0,0,0,0),"cm") ) + 
  coord_equal()
英文:

Using the circle packing library as commented by @JonSpring, code modified from documentation

df &lt;- data.frame(birds = c(&quot;Eagle&quot;, &quot;Owl&quot;, &quot;Falcon&quot;, &quot;Ostrich&quot;, &quot;Blue Jay&quot;, &quot;Chaffinch&quot;),
                 values = c(43, 23, 54, 112, 16, 12))

library(ggplot2)
install.packages(&quot;packcircles&quot;)
library(packcircles)

df$packing &lt;- circleProgressiveLayout(df$values, sizetype=&#39;area&#39;)

df.gg &lt;- circleLayoutVertices(df$packing, npoints=50)

ggplot() + 
  geom_polygon(data = df.gg, aes(x, y, group = id, fill=id), alpha = 0.6)+
  scale_fill_viridis_c()+
  geom_text(data = df, aes(x=packing$x, y=packing$y, label = birds), size=5, color=&quot;black&quot;) +
  theme_void() + 
  theme(legend.position=&quot;none&quot;, plot.margin=unit(c(0,0,0,0),&quot;cm&quot;) ) + 
  coord_equal()

气泡图,唯一的度量是大小和颜色。

Original answer:

From a reframe the question perspective, the wordcloud library can avoid overlapping text:

df &lt;- data.frame(birds = c(&quot;Eagle&quot;, &quot;Owl&quot;, &quot;Falcon&quot;, &quot;Ostrich&quot;, &quot;Blue Jay&quot;, &quot;Chaffinch&quot;),
                 values = c(43, 23, 54, 112, 16, 12))
install.packages(&quot;wordcloud&quot;)
library(wordcloud)
wordcloud(df$birds, df$values, colors=brewer.pal(8,&quot;Dark2&quot;))

气泡图,唯一的度量是大小和颜色。

huangapple
  • 本文由 发表于 2023年5月10日 22:31:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定