微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何确保两层 geom_point 正确对齐?

如何解决如何确保两层 geom_point 正确对齐?

我正在努力在 ggplot 完全 上绘制圆圈。

这是剧情代码

xx = ggplot(dfm2,aes(x = variable,y = tax)) + 
      geom_point(aes(size = sqrt(value),color = phyla),shape = 21) + 
      scale_size_continuous(limits = c(0,35),range = c(1,20)) + 
      theme(legend.key=element_blank(),axis.text.x = element_text(colour = "black",size = 12,angle = 90,vjust = 0.3,hjust = 1),axis.text.y = element_text(colour = "black",face = "bold",size = 8),legend.text = element_text(size = 10,face ="bold",colour ="black"),legend.title = element_text(size = 12,face = "bold"),panel.background = element_blank(),panel.border = element_rect(colour = "black",fill = NA,size = 1.2),legend.position = "right") 
    
    xx

我的数据框:

dfm2 <- structure(list(tax = structure(c(1L,1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,16L,17L,18L,19L,20L,21L,22L,23L,24L,24L),.Label = c("Clavulina","Cortinarius","Piloderma","Russula","Xerocomellus","Atheliaceae","Boletaceae","Clavulinaceae","Cortinariaceae","Thelephoraceae","Boletales","Cantharellales","Russulales","Cenococcum","Cladophialophora","Elaphomyces","Elaphomycetaceae","Herpotrichiellaceae","Chaetothyriales","Eurotiales","Helotiaceae","Pezizales","Sordariales","UmbelopsiDales"
),class = "factor"),test = structure(c(1L,3L),.Label = c("g","f","o"),phyla = c("Agaricomycetes","Agaricomycetes","Dothideomycetes","Eurotiomycetes","Leotiomycetes","Pezizomycetes","Sordariomycetes","Umbelopsidomycetes","Umbelopsidomycetes"
),phyla1 = c(NA,NA,NA),phyla2 = c(NA,variable = structure(c(2L,5L),.Label = c("KAC","KAF","KAW","GRC","BHC"),value = c(0,55.722420779792,16.7351551362146,130.651761638796,47.4630290758812,344.253315259547,179.599304875006,9.54521273810036,4.17856131969448,10.1906702027936,4.67675794099578,16.4389432505032,6.56146785828124,292.554112042119,192.444415108634,240.314439785311,113.567323164539,6.63551229846123,2.42876606836088,0.340378846018673,0.124587304553666,31.9637482669779,11.6995438658847,301.162822135619,166.339660106073,15.9968898677191,10.158845171958,119.003045585602,44.1241465067143,314.898113957191,115.827941884529,11.6466634606362,4.26297469592084,78.9008308713511,44.6641400711928,28.5922537488445,15.0910261581729,130.729467450933,47.6893792546978,9.54684330461525,4.19471614756479,4.99564959212625,1.37026614431018,58.2838435666646,19.6026370831945,32.8298602817313,17.9016604233309,18.5327625920274,10.4521875380684,19.4803542613531,7.60127408652835,10.2807970505094,4.71425747225164,36.5303309999698,15.4344619154041,34.5717196916383,16.9962815569886,18.5538892372794,13.4818901076747,11.7535135841283,4.30208455551645,120.936072922602,44.8317152093529,330.592974987903,137.987570972834,81.7656524037499,30.777185859533,11.8043759923957,5.97910730494584,9.59655917970584,3.92673043588229,359.029012230497,198.094178611349,240.292352409899,114.031257835764,80.00005954321,29.8311568640691,296.527500155951,250.98810836895,6.65236646087093,2.5325888221556,298.92445563979,169.429423759217,92.7194563191488,52.403869281338,126.532038941658,47.2411841059221,111.963490509424,73.9167204244325,50.4347790386522,21.1562679516175,30.3384280807787,12.0523745796255,36.7115313609751,17.9442186599375,5.35622404345806,2.4642318970895,139.58823304089,40.275439916366,71.9233658127593,23.2361293322448,9.31141084543879,2.69935890226071,33.2859896258468,15.279240235619,122.935605593244,59.1332236269826,19.0839243507556,11.0072228505803,8.67831225770859,4.80994940565981,85.8858328927752,28.3822536421699,123.949404659904,97.825605551247,33.5449967797736,17.6443721997637,23.6716255982153,16.5406062730736,35.1404871014572,21.6910522326589,153.002145966387,46.1587735277285,137.878633245296,65.7492431345792,23.8561518165481,10.8236490366025,46.2039777568,28.944729324695,159.114066407422,120.326178107811,52.2354005195345,33.6610167992512,11.2151836173417,7.02663183495428,48.8646545919587,35.3190572925153,63.0368483186293,30.8250803868248,127.444871057481,68.2274750668282,28.5426593569714,16.7932976872273,49.3303009891819,24.774068855448,15.8101618958304,10.2555714159122,1.28517298118379,0.562918778911717,31.2190858156308,17.8727837772257,7.10547842444747,4.32510661848181,21.139683447186,9.89224632770911,46.4617671152079,21.9653240368642,66.746547931994,42.9018737315692,56.0146095178226,35.5807654007847,26.8443827302432,18.3445503760626,54.8834055701279,22.5921210691608,28.1767798117358,16.3740739812223
)),row.names = c(147L,155L,156L,148L,158L,150L,159L,151L,160L,152L,195L,203L,204L,196L,206L,198L,207L,199L,208L,200L,59L,51L,60L,52L,62L,54L,63L,55L,64L,56L,169L,161L,170L,162L,171L,163L,172L,164L,176L,168L,217L,209L,218L,210L,219L,211L,220L,212L,224L,216L,73L,65L,74L,66L,75L,67L,76L,68L,72L,80L,25L,26L,27L,28L,32L,185L,177L,186L,178L,190L,182L,233L,225L,234L,226L,238L,230L,89L,81L,90L,82L,94L,86L,41L,33L,42L,34L,46L,38L,153L,145L,201L,193L,57L,49L,154L,146L,157L,149L,202L,194L,205L,197L,58L,50L,61L,53L,173L,165L,175L,167L,221L,213L,223L,215L,77L,69L,79L,71L,29L,31L,187L,179L,188L,180L,235L,227L,236L,228L,91L,83L,92L,84L,43L,35L,44L,36L,174L,166L,222L,214L,78L,70L,30L,189L,181L,237L,229L,93L,85L,45L,37L,191L,183L,239L,231L,95L,87L,47L,39L,192L,184L,240L,232L,96L,88L,48L,40L),class = "data.frame")

情节:

enter image description here

我需要让圆圈居中,其中有几个略微移动。 我已尝试多次调用 position =,但均未成功。

有什么想法吗?

会话信息:

>sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats4    parallel  stats     graphics  Grdevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.5.0               stringr_1.4.0               purrr_0.3.4                 readr_1.4.0                
 [5] tibble_3.0.4                tidyverse_1.3.0             genefilter_1.72.0           plyr_1.8.6                 
 [9] MetagenomeSeq_1.32.0        RColorBrewer_1.1-2          glmnet_4.0-2                Matrix_1.2-18              
[13] limma_3.46.0                ggrepel_0.9.0               dplyr_1.0.2                 funrar_1.4.1               
[17] qdapTools_1.3.5             dendextend_1.14.0           ggplot2_3.3.3               vegan_2.5-7                
[21] lattice_0.20-41             permute_0.9-5               reshape_0.8.8               viridis_0.5.1              
[25] viridisLite_0.3.0           tidyr_1.1.2                 DESeq2_1.30.0               SummarizedExperiment_1.20.0
[29] MatrixGenerics_1.2.0        matrixStats_0.57.0          genomicranges_1.42.0        GenomeInfoDb_1.26.2        
[33] IRanges_2.24.1              S4Vectors_0.28.1            vsn_3.58.0                  Biobase_2.50.0             
[37] Biocgenerics_0.36.0         phyloseq_1.34.0             apeglm_1.12.0              

loaded via a namespace (and not attached):
  [1] colorspace_2.0-0       ellipsis_0.3.1         XVector_0.30.0         fs_1.5.0              
  [5] rstudioapi_0.13        farver_2.0.3           affyio_1.60.0          bit64_4.0.5           
  [9] fansi_0.4.1            lubridate_1.7.9.2      AnnotationDbi_1.52.0   mvtnorm_1.1-1         
 [13] xml2_1.3.2             codetools_0.2-16       splines_4.0.3          geneplotter_1.68.0    
 [17] ade4_1.7-16            jsonlite_1.7.2         broom_0.7.3            annotate_1.68.0       
 [21] dbplyr_2.0.0           cluster_2.1.0          BiocManager_1.30.10    compiler_4.0.3        
 [25] httr_1.4.2             backports_1.2.0        assertthat_0.2.1       cli_2.2.0             
 [29] prettyunits_1.1.1      tools_4.0.3            igraph_1.2.6           coda_0.19-4           
 [33] gtable_0.3.0           glue_1.4.2             GenomeInfoDbData_1.2.4 affy_1.68.0           
 [37] reshape2_1.4.4         Rcpp_1.0.5             bbmle_1.0.23.1         cellranger_1.1.0      
 [41] vctrs_0.3.6            Biostrings_2.58.0      rhdf5filters_1.2.0     multtest_2.46.0       
 [45] ape_5.4-1              preprocesscore_1.52.1  nlme_3.1-149           iterators_1.0.13      
 [49] rvest_0.3.6            lifecycle_0.2.0        gtools_3.8.2           XML_3.99-0.5          
 [53] zlibbioc_1.36.0        MASS_7.3-53            scales_1.1.1           hms_0.5.3             
 [57] biomformat_1.18.0      rhdf5_2.34.0           memoise_1.1.0          gridExtra_2.3         
 [61] emdbook_1.3.12         bdsmatrix_1.3-4        stringi_1.5.3          Rsqlite_2.2.2         
 [65] foreach_1.5.1          caTools_1.18.0         BiocParallel_1.24.1    shape_1.4.5           
 [69] chron_2.3-56           rlang_0.4.10           pkgconfig_2.0.3        bitops_1.0-6          
 [73] Wrench_1.8.0           Rhdf5lib_1.12.0        labeling_0.4.2         bit_4.0.4             
 [77] tidyselect_1.1.0       magrittr_2.0.1         R6_2.5.0               gplots_3.1.1          
 [81] generics_0.1.0         DelayedArray_0.16.0    DBI_1.1.0              haven_2.3.1           
 [85] pillar_1.4.7           withr_2.3.0            mgcv_1.8-33            survival_3.2-7        
 [89] RCurl_1.98-1.2         modelr_0.1.8           Crayon_1.3.4           KernSmooth_2.23-17    
 [93] progress_1.2.2         readxl_1.3.1           locfit_1.5-9.4         grid_4.0.3            
 [97] data.table_1.13.6      blob_1.2.1             reprex_0.3.0           digest_0.6.27         
[101] xtable_1.8-4           numDeriv_2016.8-1.1    munsell_0.5.0  

   

解决方法

绘图到屏幕时,我在 Windows 上的 R 中看到了相同的行为。但是,我发现使用 ggsave() 保存的文件中不会出现此问题。这可能是您的用例的一个选项。

,

这很有趣,我无法复制这个问题。当我将您的数据与您的代码一起使用时,我得到以下图:

enter image description here

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.3.2

loaded via a namespace (and not attached):
 [1] rstudioapi_0.11  magrittr_2.0.1   tidyselect_1.1.0 munsell_0.5.0    colorspace_1.4-1 R6_2.5.0         rlang_0.4.10    
 [8] blob_1.2.1       dplyr_1.0.3      tools_4.0.3      grid_4.0.3       packrat_0.5.0    gtable_0.3.0     DBI_1.1.0       
[15] withr_2.2.0      ellipsis_0.3.1   digest_0.6.27    assertthat_0.2.1 tibble_3.0.5     lifecycle_0.2.0  crayon_1.3.4    
[22] purrr_0.3.4      farver_2.0.3     vctrs_0.3.6      glue_1.4.2       labeling_0.3     compiler_4.0.3   pillar_1.4.7    
[29] generics_0.1.0   scales_1.1.1     pkgconfig_2.0.3 
> 
,

与我的 Windows 机器上的 Mathilde 的结果相同。

如 Miff 所述,使用 ggsave 修复

enter image description here

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.3.3

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13  magrittr_2.0.1   tidyselect_1.1.0 munsell_0.5.0   
 [5] colorspace_2.0-0 R6_2.5.0         rlang_0.4.9      stringr_1.4.0   
 [9] dplyr_1.0.2      tools_4.0.3      grid_4.0.3       gtable_0.3.0    
[13] xfun_0.19        tinytex_0.28     withr_2.3.0      ellipsis_0.3.1  
[17] digest_0.6.27    tibble_3.0.4     lifecycle_0.2.0  crayon_1.3.4    
[21] purrr_0.3.4      farver_2.0.3     vctrs_0.3.6      glue_1.4.2      
[25] labeling_0.4.2   stringi_1.5.3    compiler_4.0.3   pillar_1.4.7    
[29] generics_0.1.0   scales_1.1.1     pkgconfig_2.0.3 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。