美文网首页
ggplot2绘制纹理填充图

ggplot2绘制纹理填充图

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

    分享一个用纹理填充柱状图的小例子,喜欢的小伙伴可以关注个人公众号R语言数据分析指南,持续分享更多优质资源

    devtools::install_github("clauswilke/ggtextures")
    library(ggtextures)
    library(grid)
    library(magick)
    library(tidyverse)
    
    df <- tibble(
      trt = c("a", "b", "c"),
      outcome = c(2.3, 1.9, 3.2),
      image = c(
        "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks2-256.jpg",
        "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/stone2-256.jpg",
        "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/siding1-256.jpg"))
    
    ggplot(df, aes(trt, outcome, image = image)) +
      geom_textured_col(img_width = unit(0.3, "null"))+
      theme_bw()+
      theme_minimal()+
      theme(axis.title.x=element_blank(),
            axis.ticks.y=element_blank(),
            axis.ticks.x=element_blank(),
            legend.title=element_blank())
      scale_y_continuous(expand=c(0,0))
    
      images = c(
        compact = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks2-256.jpg",
        midsize = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/stone2-256.jpg",
        suv = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/siding1-256.jpg",
        `2seater` = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/mulch1-256.jpg",
        minivan = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks1-256.jpg",
        pickup = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/wood3-256.jpg",
        subcompact = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/concrete1-256.jpg"
      )
      
      ggplot(mpg, aes(class, image = class)) +
        geom_textured_bar() +
        scale_image_manual(values = images)+
        theme_minimal()+
        theme(axis.title.x=element_blank(),
              axis.ticks.y=element_blank(),
              axis.ticks.x=element_blank(),
              legend.title=element_blank())+
      scale_y_continuous(expand=c(0,0))
    
      ggplot(mpg, aes(factor(trans), image = class)) +
        geom_textured_bar() +
        scale_image_manual(values = images)+
        theme_minimal()+
        theme(axis.title.x=element_blank(),
              axis.ticks.y=element_blank(),
              axis.ticks.x=element_blank(),
              legend.title=element_blank(),
              axis.text.x =element_text(angle =90,
                                        hjust =0.5,vjust = 0.5))+
        scale_y_continuous(expand=c(0,0))
    

    链接:https://mp.weixin.qq.com/s/vR_wNHLrEnWG13wa0pcThA

    相关文章

      网友评论

          本文标题:ggplot2绘制纹理填充图

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