ggplot2繪制面積圖

本節(jié)使用TidyTuesday 2021 第15周的項目數(shù)據(jù)集來分析導致巴西森林流失的主要原因,后臺回復關鍵詞2021-15獲取數(shù)據(jù)

加載R包

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

數(shù)據(jù)清洗

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")))

數(shù)據(jù)可視化

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))
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容