美文网首页
ggplot2绘制面积图

ggplot2绘制面积图

作者: R语言数据分析指南 | 来源:发表于2021-04-07 22:51 被阅读0次

本节使用TidyTuesday 2021 第15周的项目数据集来分析导致巴西森林流失的主要原因,后台回复关键词2021-15获取数据

加载R包

library(tidyverse)
library(extrafont)
library(ggstream)
library(scales)
library(ggtext)

数据清洗

brazil_loss <- read.delim("brazil_loss.xls",sep="\t")

brazil_loss_long <- 
  brazil_loss %>% 
  pivot_longer(4:14, names_to = "types", values_to = "loss")
labels <- tibble(year = 2013.1,
       loss = c(1000000, 590000, 340000, 200000, 70000),
       types = c("Pasture", "Small Scale Clearing",
       "Commercial Crops", "Fire", "Selective Logging"),
       colores = c("#0091AD", "#2E6F95",
       "#5C4D7D", "#892B64", "#B7094C"))

brazil_loss_long <- brazil_loss_long %>% 
  filter(types %in% c("pasture",
  "small_scale_clearing", "commercial_crops",
  "fire", "selective_logging")) %>% 
  mutate(types = factor(types,
  levels = c("pasture", "small_scale_clearing",
  "commercial_crops", "fire", "selective_logging")))

数据可视化

ggplot() +
  geom_stream(brazil_loss_long, 
              mapping = aes(year,loss,fill = types),
              extra_span = .1,type = "ridge",
              show.legend = FALSE) +
  scale_y_continuous(labels = label_number_si()) +
  geom_text(labels,mapping = aes(year,loss,
  label = types,color = colores),
  hjust = 0,vjust = 1,
  family = "IBM Plex Sans",size=1.5) +
  scale_x_continuous(breaks = seq(2001,2015, 3),
  limits = c(2001, 2015)) +
  scale_fill_manual(values = c("#0091AD",
  "#2E6F95", "#5C4D7D", "#892B64", "#B7094C")) +
  scale_color_identity() +
  labs(x = NULL, y = NULL) +
  coord_cartesian(expand = FALSE, clip = "off") +
  theme(panel.background = element_rect("#F5F5F5"),
        plot.background = element_rect("#F5F5F5"),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        axis.text.x = element_text(size=5),
        axis.text.y=element_text(size=5))

相关文章

网友评论

      本文标题:ggplot2绘制面积图

      本文链接:https://www.haomeiwen.com/subject/vsbpkltx.html