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轴双坐标

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

  • Path.addCircle(记录用,待编辑)

    float x:圆心X轴坐标float y:圆心Y轴坐标float radius:圆半径Path.Directio...

  • R 绘制双坐标轴

    双坐标轴其实实用性并不大,而且也有文献指出了他的可读性不强,但是由于小伙伴的需求Poesie还是去研究了关于R 绘...

  • Appium+Python swipe 用法详解

    API start_x - 滑动开始x轴坐标 start_y - 滑动开始y轴坐标 end_x - 滑动结...

  • 小白学opengl 第四课

    第四课:旋转 坐标轴遵循右手坐标系 x轴旋转 y轴旋转 z轴旋转

  • 1.空间直角坐标系

    1.空间直角坐标系 1.1空间直角坐标系的建立 坐标轴:x轴(横轴)、y轴(纵轴)、z轴(竖轴)。 空间直角坐标系...

  • three.js(3)-坐标轴与物体

    坐标轴 初始化坐标轴: 这个坐标轴跟我们数学学过的立体坐标系有点区别: 其中x轴是红色,y轴是绿色,那z轴哪去了?...

  • 06.旋转

    坐标P(x,y,z) , 绕Z轴旋转β角度变成 p1(x1, y1, z1) 推理出来: cos a = x/r;...

  • 2018-09-26 用ggplot2标记时间序列线的末端

    利用ggplot2的第二个Y轴(双坐标轴)标记折线图的末端。 原文见https://drsimonj.svbtle...

  • echarts 坐标轴

    option = {xAxis : {}, // x轴设置yAxis : {}, // y轴设置} 坐标轴分割线...

网友评论

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

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