美文网首页重点关注绘图技巧
ggplot2可视化经典案例系列(二)之柱状图

ggplot2可视化经典案例系列(二)之柱状图

作者: R语言数据分析指南 | 来源:发表于2021-01-27 23:01 被阅读0次

    一个简简单单的柱状图也可以如此优雅,还在使用各种在线工具嘛,还在羡慕别人的图表吗,科研人赶紧动起来吧。跟着我关注个人公众号R语言数据分析指南持续分享国际一流数据分析师的经典项目案例,带你站在潮头之上

    library(tidytuesdayR)
    library(tidyverse)
    library(hablar)
    

    TidyTuesdayR4DS在线学习社区的一个项目,在该项目中,他们将每周数据集发布到公共数据存储库tidytuesday中,供人们分析和可视化

    tidytuesdayR包可以轻松的访问每周的TidyTuesday项目数据集;

    安装tidytuesdayR

    install.packages("tidytuesdayR")
    
    remotes::install_github("thebioengineer/tidytuesdayR")
    

    数据加载

    rm(list=ls())
    tuesdata <- tidytuesdayR::tt_load('2021-01-26')
    
    plastics <- tuesdata$plastics
    
    head(plastics)
    

    数据清理

    plastics_tidy <- plastics %>% 
      pivot_longer(names(plastics)[4:11],
                   names_to = "type",
                   values_to = "count")
    
    head(plastics_tidy)
    
    usa <- plastics_tidy %>%
      filter(country == "United States of America") %>%
      filter(year == "2020") %>%
      filter(parent_company != "null") %>% 
      filter(parent_company != "Unbranded") %>%
      filter(grand_total > 100) %>%
      convert(fct(parent_company,type))
    
    unique(usa$parent_company)
    

    ggplot2数据可视化

    ggplot(usa,aes(x = reorder(parent_company,-count),
    y = count,group = parent_company,fill = type)) +
      geom_bar(stat = "identity",
               position = "stack") +
      labs(title = "Which companies are the largest sources of branded plastic pollution in the USA?",
           fill = "Type of Plastic",
           caption = "TidyTuesday Week 5") +
      ylab("Count of plastic") +xlab(NULL) +
      scale_fill_brewer(palette = "Spectral") +
      theme(plot.background = element_rect(fill = "grey10"),
            panel.background = element_rect(fill = "grey10"),
            panel.grid = element_blank(),
            axis.ticks = element_blank(),
            plot.title = element_text(size = 17.5,
            colour = "grey90",face = "bold"),
            title = element_text(colour = "grey50"),
            axis.title.y = element_text(size = 10,
            hjust = 0.52,colour = "grey90"),
            axis.text.y = element_text(colour = "grey90"),
            axis.text.x = element_blank(),
            legend.title = element_text(size = 10, colour = "white"),
            legend.text = element_text(colour = "grey90"),
            legend.background = element_rect(fill = "grey10"),
            legend.position = c(0.8, 0.9),
            legend.direction = "horizontal") +
      guides(fill = guide_legend(reverse = T)) +
      annotate("text", x = 1, y = -20, label = "The Kroger
    Company",colour = "#084999",size = 4) +
      annotate("text", x = 2, y = -15, label = "Pepsico",
    colour = "#28458E",size = 4) +
      annotate("text", x = 3, y = -15, label = "CocaCola",
               colour = "#F40009",size = 4) +
      annotate("text", x = 4, y = -15,label = "Nestle",
               colour = "#ddc694",size = 4) +
      annotate("text", x = 5, y = -15, label = "STARBUCKS",
               colour = "#00704A",size = 4) +
      annotate("text", x = 6, y = -15, label = "FAGE",
               colour = "#0053A0",size = 4) +
      annotate("text", x = 7, y = -14,
               label = "phillip morris",colour = "#4ea2da",
    size = 4) +
      annotate("text", x = 8, y = -14,label = "SUNTORY",
               colour = "#5bc2dc",size = 4) +
      annotate("text",x = 3.6, y = 340,
               label = "PET plastic is a polyester
    and is commonly used to 
    make plastic drinks bottles
    and food containers",size = 4,
               colour = "#c3d674") +
      annotate("curve",xend = 2.3, yend = 280,
               x = 2.9, y = 315,curvature = -.2,
               arrow = arrow(type = "closed", 
                             length = unit(0.40, "lines")),
               colour = "#7b8749") +
      annotate("curve",xend = 3.1, yend = 200,
               x = 3.3, y = 290,curvature = -.2,
               arrow = arrow(type = "closed", 
                             length = unit(0.40, "lines")),
               colour = "#7b8749") +
      annotate("curve",xend = 4, yend = 180,
               x = 4.2, y = 290,curvature = -.2,
               arrow = arrow(type = "closed", 
                             length = unit(0.40, "lines")),
               colour = "#7b8749") +
      annotate("text",x = 5.6, y = 240,
               label = "PP Plastic is a 
    polypropeylene and is used
    to make products such as 
    yoghurt containers and 
    carry out beverage cups",
               size = 4, colour = "#9DDF9E") +
      annotate("curve",
               xend = 6.1, yend = 100,x = 6.1, y = 180,
               curvature = -.2,
               arrow = arrow(type = "closed", 
                             length = unit(0.40, "lines")),
               colour = "#74a375") +
      annotate("curve",xend = 4.8, yend = 100,
               x = 4.8, y = 210,curvature = .2,
               arrow = arrow(type = "closed", 
                             length = unit(0.40, "lines")),
               colour = "#74a375") +
      annotate("curve",xend = 1.5, yend = 470,
               x = 5.1, y = 300,curvature = .3,
               arrow = arrow(length = unit(0.40, "lines")),
               colour = "#74a375") 
    
    image

    参考:https://mp.weixin.qq.com/s/oC8f9wGeDIdlU0ev-q8kug

    相关文章

      网友评论

        本文标题:ggplot2可视化经典案例系列(二)之柱状图

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