英文:
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 <- c("Eagle", "Owl", "Falcon", "Ostrich", "Blue Jay", "Chaffinch")
values <- c(43, 23, 54, 112, 16, 12)
df <- 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 <- 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()
Original answer:
From a reframe the question perspective, the wordcloud
library can avoid overlapping text:
df <- data.frame(birds = c("Eagle", "Owl", "Falcon", "Ostrich", "Blue Jay", "Chaffinch"),
values = c(43, 23, 54, 112, 16, 12))
install.packages("wordcloud")
library(wordcloud)
wordcloud(df$birds, df$values, colors=brewer.pal(8,"Dark2"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论