📜  ggplot abline thickness - R 编程语言(1)

📅  最后修改于: 2023-12-03 15:15:15.669000             🧑  作者: Mango

ggplot abline thickness - R 编程语言

简介

ggplot2 是一款功能强大的绘图包,常用于数据可视化。其中,abline 函数用于添加一条直线到图表中。本文将介绍如何在 ggplot2 的图表中调整 abline 的粗细。

准备数据

在介绍如何调整 abline 的粗细之前,我们需要准备一些数据来绘制图表。以下是一组简单的数据,代表年龄和收入的关系。

set.seed(123)
age <- sample(18:65, 50, replace = TRUE)
income <- 5000 + 50*age + rnorm(50, sd = 2000)
df <- data.frame(age = age, income = income)
绘制图表

使用 ggplot2 绘制散点图。

library(ggplot2)
ggplot(df, aes(x = age, y = income)) + geom_point()

scatterplot

添加 abline

添加一条 abline 并调整其粗细。我们可以使用参数 size 或者 lwd 来调整粗细,两者等价。

ggplot(df, aes(x = age, y = income)) + 
  geom_point() + 
  geom_abline(slope = 50, intercept = 5000, size = 1.5)

abline-size

ggplot(df, aes(x = age, y = income)) + 
   geom_point() + 
   geom_abline(slope = 50, intercept = 5000, lwd = 1.5)

abline-lwd

总结

本文介绍了如何在 ggplot2 图表中调整 abline 的粗细。我们可以使用 size 或者 lwd 参数来调整线条粗细。ggplot2 的强大功能不需要更多的解释,通过绘图实例可以快速入门。

sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin20.5.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
BLAS:   /Applications/R.app/Contents/Resources/lib/libRblas.dylib
LAPACK: /Applications/R.app/Contents/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] ggplot2_3.3.5

loaded via a namespace (and not attached):
 [1] colorspace_2.0-2      ellipsis_0.3.2        rprojroot_2.0.2      
 [4] utf8_1.2.1            tinytex_0.32          gridExtra_2.3        
 [7] fs_1.5.0              rstudioapi_0.13       farver_2.1.0         
[10] remotes_2.4.0         bit64_4.0.5           fansi_0.4.2          
[13] lubridate_1.7.10      xml2_1.3.2            codetools_0.2-18     
[16] splines_4.1.0         mnormt_2.0.2          jsonlite_1.7.2       
[19] broom_0.7.9           compiler_4.1.0        httr_1.4.2           
[22] backports_1.2.1       assertthat_0.2.1      fastmap_1.1.0        
[25] cli_3.0.1             prettyunits_1.1.1     tools_4.1.0          
[28] igraph_1.2.6          gtable_0.3.0          glue_1.4.2           
[31] reshape2_1.4.4        Rcpp_1.0.7            vctrs_0.3.8          
[34] cellranger_1.1.0      nlme_3.1-152          scatterplot3d_0.3-41 
[37] irlba_2.3.3           xfun_0.24             rvest_1.0.1          
[40] lifecycle_1.0.0       XML_3.99-0.6          MASS_7.3-54          
[43] zlibbioc_1.38.0       scales_1.1.1          hms_1.1.0            
[46] RColorBrewer_1.1-2    curl_4.3.2            grid_4.1.0           
[49] rpart_4.1-15          sjlabelled_1.1.8-1    rmutils_2.4.3        
[52] sjmisc_2.8.6          ggplot2movies_0.0.1   magrittr_2.0.1       
[55] rsq_1.0.9             matrixStats_0.59.0    lmtest_0.9-38        
[58] R6_2.5.1              generics_0.1.0        sjPlot_2.8.7         
[61] IRdisplay_1.0         multcomp_1.4-16       parallel_4.1.0       
[64] mgcv_1.8-38           zeallot_0.1.0         yaml_2.2.1           
[67] abind_1.4-5           stringi_1.7.4         zip_2.2.0            
[70] pkgconfig_2.0.3       utf8lite_0.4.2        reshape_0.8.8        
[73] labeling_0.4.2        RSQLite_2.2.8         plyr_1.8.6           
[76] magick_2.7.3          dplyr_1.0.7           sjstats_0.19.2       
[79] survival_3.2-11       purrr_0.3.4           ggrepel_0.9.1        
[82] ggpubr_0.4.0          corpcor_1.6.12        tidyr_1.1.3          
[85] markdown_1.1          shiny_1.7.1           farff_2.0            
[88] pROC_1.18.0           vroom_1.5.4           sjtutils_2.0.3       
[91] dbplyr_2.1.1          readxl_1.3.1          data.table_1.14.0    
[94] png_0.1-7             reprex_2.0.1          bit_4.0.4            
[97] compiler_4.1.0        R.methodsS3_1.8.1     tibble_3.1.3         
[100] statmod_1.4.34        crayon_1.4.1          sjlabelledData_2.2.1 
[103] gridGraphics_0.5-1    DBI_1.1.1             ggsci_2.9            
[106] boot_1.3-28           sjstats_0.19.2        dbplot_0.4.3         
[109] Formula_1.2-4         sjmiscData_2.6.3      threejs_0.3.4        
[112] MASS_7.3-54           patchwork_1.1.1       foreign_0.8-81