R可视化:y轴双坐标

作者: 生信学习者2 | 来源:发表于2020-11-16 22:59 被阅读0次

    今天遇到需要双坐标的y轴需求,上Stack Overflow正好搜索到该结果,特记录下来。更多知识分享请到 https://zouhua.top/

    导入数据

    library(tidyverse)
    library(data.table)
    dt.diamonds <- as.data.table(diamonds)
    

    处理数据

    d1 <- dt.diamonds[,list(revenue = sum(price),
                            stones = length(price)),
                      by=c("clarity","cut")]
    
    max_stones <- max(d1$stones)
    max_revenue <- max(d1$revenue)
    
    d2 <- gather(d1, 'var', 'val', stones:revenue) %>% 
                  mutate(val = if_else(var == 'revenue', as.double(val), val / (max_stones / max_revenue))) 
    

    作图

    通过sec.axis构造双y轴坐标ggplot2图

    ggplot(data = d2, aes(x = clarity, y = val))+
      geom_bar(aes(fill = cut), filter(d2, var == 'revenue'), stat = 'identity')+
      geom_point(data = filter(d2, var == 'stones'), col = 'red')+
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * (max_stones / max_revenue),
                                             name = 'number of stones')) +
      labs(x="", y="revenue")+
      facet_grid(~cut)+
      theme_bw()+
      theme(axis.text.x = element_text(angle = 90, hjust = 1),
            axis.text.y = element_text(color = "#4B92DB"),
            axis.text.y.right = element_text(color = "red"),
            legend.position="bottom") 
    

    R information

    sessionInfo()
    
    R version 4.0.3 (2020-10-10)
    Platform: x86_64-apple-darwin17.0 (64-bit)
    Running under: macOS Big Sur 10.16
    
    Matrix products: default
    LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
    
    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
     [1] data.table_1.13.2 forcats_0.5.0     stringr_1.4.0     dplyr_1.0.2       purrr_0.3.4       readr_1.4.0       tidyr_1.1.2      
     [8] tibble_3.0.4      ggplot2_3.3.2     tidyverse_1.3.0  
    
    loaded via a namespace (and not attached):
     [1] Rcpp_1.0.5       cellranger_1.1.0 pillar_1.4.6     compiler_4.0.3   dbplyr_2.0.0     tools_4.0.3      digest_0.6.27   
     [8] lubridate_1.7.9  jsonlite_1.7.1   evaluate_0.14    lifecycle_0.2.0  gtable_0.3.0     pkgconfig_2.0.3  rlang_0.4.8     
    [15] reprex_0.3.0     cli_2.1.0        DBI_1.1.0        rstudioapi_0.12  yaml_2.2.1       haven_2.3.1      xfun_0.19       
    [22] withr_2.3.0      xml2_1.3.2       httr_1.4.2       knitr_1.30       fs_1.5.0         hms_0.5.3        generics_0.1.0  
    [29] vctrs_0.3.4      grid_4.0.3       tidyselect_1.1.0 glue_1.4.2       R6_2.5.0         fansi_0.4.1      readxl_1.3.1    
    [36] rmarkdown_2.5    modelr_0.1.8     magrittr_1.5     backports_1.2.0  scales_1.1.1     ellipsis_0.3.1   htmltools_0.5.0 
    [43] rvest_0.3.6      assertthat_0.2.1 colorspace_2.0-0 stringi_1.5.3    munsell_0.5.0    broom_0.7.2      crayon_1.3.4   
    

    reference

    1. How to use facets with a dual y-axis ggplot

    参考文章如引起任何侵权问题,可以与我联系,谢谢。

    相关文章

      网友评论

        本文标题:R可视化:y轴双坐标

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