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

2022-05-23 15:59:34 浏览数 (1)

数据代码来源

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 innregions with poor infrastructure. Countries with low income never saw the rise of landline phones, butnachieved widespread use of cell phones within two decades",
       x = "nLandline Subscriptions pr. 100 people",
       y = "Cell Phone Subscriptions pr. 100 peoplen",
       caption = "Data Source: World Bank | #30daychartchallengen@emilmalta"
  )
dev.off()

image.png

0 人点赞