这个代码是利用gganimate这个R包做了一个动态的气泡图,结果非常惊艳,可以点卡下方视频观看一下

今天的推文主要学习其中做气泡图的代码和非常舒服的配色

加载需要用到的R包

library(tidyverse)
library(gganimate)
library(ggplot2)

读取数据集

这里我只选取原始代码数据集中的一小部分

dat01<-readr::read_csv("20220522.csv")

准备配色

cols <- c(
"Africa" = "#34a186",
"Americas" = "#f9cb45",
"Asia" = "#b5182b",
"Europe" = "#4cb1c4",
"Oceania" = "#ab96d2"
)

作图代码

pdf(file = "abc1.pdf",
width = 20,height = 20)
dat01 %>%
arrange(desc(sp_pop_totl)) %>%
ggplot(aes(x = it_mlt_main_p2,
y = it_cel_sets_p2,
label = country)) +
geom_point(
aes(size = sp_pop_totl, fill = continent),
pch = 21, color = "white", alpha = .9
)+
scale_fill_manual(values = cols) +
scale_size_area(max_size = 80) +
coord_cartesian(xlim = c(0, 75), ylim = c(0, 200), clip = "off") +
guides(size = "none") +
theme_minimal(base_family = "serif", base_size = 35) +
theme(
text = element_text(color = "#3a3e4c"),
plot.background = element_rect(fill = "#f2ebe7", color = NA),
panel.grid = element_line(linetype = 3, color = "#c9c8c4"),
plot.title.position = "plot",
plot.title = element_text(size = 100),
plot.subtitle = element_text(family = "serif"),
legend.position = "bottom", plot.margin = margin(20,20,20,20)
) +
guides(size = "none", fill = guide_legend(title = NULL, override.aes = list(size = 5))) +
labs(title = "Rise of Cell Phones",
subtitle = "Cell phones are not only more convenient for the end user. They are also much easier to implement in\nregions with poor infrastructure. Countries with low income never saw the rise of landline phones, but\nachieved widespread use of cell phones within two decades",
x = "\nLandline Subscriptions pr. 100 people",
y = "Cell Phone Subscriptions pr. 100 people\n",
caption = "Data Source: World Bank | #30daychartchallenge\n@emilmalta"
)
dev.off()

R语言ggplot2一幅好看的气泡图及非常舒服的配色_ide

image.png

示例数据和代码可以 点赞 点击在看 然后后台回复20220522获取

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!