前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言ggplot2一幅好看的气泡图及非常舒服的配色

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

作者头像
用户7010445
发布2022-05-23 15:59:34
2.4K0
发布2022-05-23 15:59:34
举报

数据代码来源

https://github.com/emilmalta/30daychartchallenge/blob/master/script/22_animation.R

这个代码是利用gganimate这个R包做了一个动态的气泡图,结果非常惊艳

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

加载需要用到的R包

代码语言:javascript
复制
library(tidyverse)
library(gganimate)
library(ggplot2)

读取数据集

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

代码语言:javascript
复制
dat01<-readr::read_csv("20220522.csv")

准备配色

代码语言:javascript
复制
cols <- c(
  "Africa" = "#34a186",
  "Americas" = "#f9cb45",
  "Asia" = "#b5182b",
  "Europe" = "#4cb1c4",
  "Oceania" = "#ab96d2"
)

作图代码

代码语言:javascript
复制
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()

image.png

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 数据代码来源
  • 加载需要用到的R包
  • 读取数据集
  • 准备配色
  • 作图代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档