R+ggplot2:将对数刻度线添加到直方图

ovfsdjhp  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(104)

请看一下文章末尾的reprex。我生成了一些对数正态分布的值,然后使用非均匀的bin对分布进行bin(如果取其对数,网格是均匀分布的)。重点不是数学,而是使用annotation_logticks(请参见
https://ggplot2.tidyverse.org/reference/annotation_logticks.html
)我无法将刻度添加到图中。
有人知道哪里出问题了吗?多谢!

library(tidyverse)
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor

## auxiliary functions

scale_x_log10nice <- function(name=NULL,omag=seq(-20,20),...) {
    breaks10 <- 10^omag
    scale_x_log10(name,breaks=breaks10,
                  labels=scales::trans_format("log10", scales::math_format(10^.x)),...)
}

log_binning <- function(x_min,x_max,n_bin){
x_max <- x_max
m <- n_bin-1
r <- (x_max/x_min)^(1/m)
my_seq <- seq(0,m,by=1)
grid <- x_min*r^my_seq

}

##################################################à

set.seed(1234)

n_bins <- 10

df <- tibble(x=rlnorm(10e4, sdlog=2))

my_breaks2 <- log_binning(min(df$x),
                          max(df$x), n_bins)


gpl <- ggplot(df, aes(x=x )) +
theme_bw()+

geom_histogram(## binwidth=10e3,
               colour="black", fill="blue"## , boundary=0
               ,  breaks=my_breaks2
               )+
    
    scale_x_log10nice("x values")

gpl

gpl2 <- gpl+
    annotation_logticks(sides="b", outside=T)

## where are the logticks?

gpl2

sessionInfo()
#> R version 4.2.2 (2022-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 11 (bullseye)
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#>  [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#>  [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] scales_1.2.1      forcats_0.5.2     stringr_1.5.0     dplyr_1.0.99.9000
#>  [5] purrr_1.0.0       readr_2.1.3       tidyr_1.2.1       tibble_3.1.8     
#>  [9] ggplot2_3.4.0     tidyverse_1.3.2  
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_1.2.0    xfun_0.36           haven_2.5.1        
#>  [4] gargle_1.2.1        colorspace_2.0-3    vctrs_0.5.1        
#>  [7] generics_0.1.3      htmltools_0.5.4     yaml_2.3.6         
#> [10] utf8_1.2.2          rlang_1.0.6         pillar_1.8.1       
#> [13] glue_1.6.2          withr_2.5.0         DBI_1.1.3          
#> [16] dbplyr_2.2.1        readxl_1.4.1        modelr_0.1.10      
#> [19] lifecycle_1.0.3     munsell_0.5.0       gtable_0.3.1       
#> [22] cellranger_1.1.0    rvest_1.0.3         evaluate_0.19      
#> [25] labeling_0.4.2      knitr_1.41          tzdb_0.3.0         
#> [28] fastmap_1.1.0       fansi_1.0.3         highr_0.10         
#> [31] broom_1.0.2         backports_1.4.1     googlesheets4_1.0.1
#> [34] jsonlite_1.8.4      farver_2.1.1        fs_1.5.2           
#> [37] hms_1.1.2           digest_0.6.31       stringi_1.7.8      
#> [40] grid_4.2.2          cli_3.6.0           tools_4.2.2        
#> [43] magrittr_2.0.3      crayon_1.5.2        pkgconfig_2.0.3    
#> [46] ellipsis_0.3.2      xml2_1.3.3          reprex_2.0.2       
#> [49] googledrive_2.0.0   lubridate_1.9.0     timechange_0.1.1   
#> [52] assertthat_0.2.1    rmarkdown_2.19      httr_1.4.4         
#> [55] R6_2.5.1            compiler_4.2.2

创建于2023年1月17日,使用reprex v2.0.2

9lowa7mx

9lowa7mx1#

如果要在annotation_logticks中使用outside = TRUE,还需要关闭剪裁。
来自?annotation_logticks的文档

外面 控制是否将日志刻度移出绘图区的逻辑。默认值为off(FALSE)。您还需要使用coord_cartesian(clip = "off")

gpl + 
  annotation_logticks(sides="b", outside = TRUE) +
  coord_cartesian(clip = "off")

相关问题