简体   繁体   中英

R+ggplot2: adding log tick marks to a histogram

Please have a look at the reprex at the end of the post. I generate some lognormally distributed values and then I bin the distribution using a non-uniform bin (the grid is evenly spaced if I take its logarithm). The point is not the maths, but the fact that, using annotation_logticks ( see

https://ggplot2.tidyverse.org/reference/annotation_logticks.html

) I cannot add the ticks to the plot.

Does anybody understand what goes wrong? Thanks a lot!



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

Created on 2023-01-17 with reprex v2.0.2

If you want to use outside = TRUE in annotation_logticks , you also need to turn clipping off.

From the docs for ?annotation_logticks

outside logical that controls whether to move the log ticks outside of the plot area. Default is off (FALSE). You will also need to use coord_cartesian(clip = "off")

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM