scale_y_log axis labels

1,513 views
Skip to first unread message

Jacob Wegelin

unread,
Mar 14, 2012, 7:19:57 PM3/14/12
to ggp...@googlegroups.com

With a log-scale axis, is there a simple way to get axis labels ("axis text") in a form that a layperson can easily read, rather than as an exponential expression?

I mean axis labels like what you get in the call to base graphics below.

junk<-data.frame(x=-3:2)
junk$y<- exp(junk$x^2)

# This labels the y axis in a way that a non-statistician can understand:
plot( y~ x, log="y", data=junk)
require(ggplot2)

# This labels the y axis in a way that only someone with mathematical
# training can understand:
print( ggplot(data=junk, aes(x=x, y=y)) + geom_point() + scale_y_log())

Thanks for any insights

Jacob Wegelin

Dennis Murphy

unread,
Mar 14, 2012, 8:33:59 PM3/14/12
to Jacob Wegelin, ggp...@googlegroups.com
Hi:

I would submit that your y-axis is misleading because it has equally
(linearly) spaced tick marks and nonlinear axis labels, and therefore
would likely confuse rather than edify a layperson. (I don't know many
non-mathematicians/scientists who can mentally interpolate values such
as 125 or 500 on such scales.) Here are a couple of ways to get what
you want, but I prefer the exponential labels because at least they
clarify that each break represents a power of 10. (BTW, this assumes
ggplot2-0.9.0 with the scales package loaded.)

library('ggplot2')
library('scales')

ggplot(junk, aes(x = x, y = y)) + geom_point() +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x))

ggplot(junk, aes(x = x, y = y)) + geom_point() +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))

trans_breaks() and trans_format() are functions from the scales
package, as is math_format(). Functions that end in _breaks in the
scales package are meant to be used in conjunction with the breaks =
argument in a scale_*() function call, whereas functions that end in
_format are meant to be used with the labels = argument. The _format()
functions in the scales package replace and generalize the formatter =
argument in previous versions of ggplot2.

Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Jacob Wegelin

unread,
Mar 14, 2012, 8:59:43 PM3/14/12
to Dennis Murphy, ggp...@googlegroups.com

Thank you. But both your pieces of code return errors:

> ggplot(junk, aes(x = x, y = y)) + geom_point() +
+ scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x))

Error in match.fun(get(".transform", .))(values) :
Non-numeric argument to mathematical function

> ggplot(junk, aes(x = x, y = y)) + geom_point() +

+ scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),

+ labels = trans_format("log10", math_format(10^.x)))
Error in match.fun(get(".transform", .))(values) :
Non-numeric argument to mathematical function

> sessionInfo()
R version 2.14.2 Patched (2012-02-29 r58604)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

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] grid splines stats graphics grDevices utils datasets methods base

other attached packages:
[1] scales_0.2.0 ggplot2_0.9.0 proto_0.3-9.2 reshape_0.8.4 plyr_1.6 nlme_3.1-103

loaded via a namespace (and not attached):
[1] colorspace_1.1-0 dichromat_1.2-4 digest_0.5.1 lattice_0.20-0 munsell_0.3 psych_1.1.11 RColorBrewer_1.0-5 stringr_0.5
[9] tools_2.14.2

What's wrong?

Jacob Wegelin

Dennis Murphy

unread,
Mar 14, 2012, 11:51:04 PM3/14/12
to Jacob Wegelin, ggp...@googlegroups.com
Are you using ggplot-0.8.9 or 0.9.0? They work in the latter.

D.

marios...@gmail.com

unread,
Jul 23, 2018, 9:01:11 AM7/23/18
to ggplot2
Hello, 

I have a similar problem :

typeof(power.exponent.tfail.HRS.1.pct.HRS$t.fail.yrs)
[1] "double"
> power.exponent.tfail.HRS.50.pct.plot <- ggplot(power.exponent.tfail.HRS.50.pct.HRS, aes( Si.pct, t.fail.yrs, fill= wafer, group= Probability )) +
+     geom_bar(stat = "identity", width=0.8, position = position_dodge(width=1.5))+
+     geom_hline(yintercept = 10)+
+     geom_hline(yintercept = 5, linetype = "dashed")+
+     #geom_hline(yintercept = 0.00273973*8760, linetype = "dotted")+
+     #labs(x = "Splits", y= as.expression(bquote(~tau['fail']~' @ '~.(given.temperature)~'°C (years)'))  ) +
+     labs(x = "Splits", y= as.expression(bquote(~t['fail'] ~ ' (years)')), title= "HRS Median Time to fail"  ) +
+     scale_y_log10(breaks=trans_format('log10',math_format(10^.x)), 
+                   labels=trans_format('log10',math_format(10^.x)), 
+                   limits=c(10^(floor(log10(min( power.exponent.tfail.HRS.50.pct.HRS$t.fail.yrs  ) ))), 
+                            10^(ceiling(log10(max(power.exponent.tfail.HRS.50.pct.HRS$t.fail.yrs ) ))) ) )+
+     annotation_logticks(sides = "lr")+
+     scale_fill_viridis(option="B", discrete = TRUE, begin = 0.0, end = 0.85, direction = 1, name="Wafer") +
+     scale_x_discrete(labels = function(x) str_wrap(x, width = 7))+
+     theme_thesis(choose.font.family, axis.text = 23, axis.title.text = 23, strip.wrap.size = 23)+
+     theme(legend.position = "none")+
+     facet_wrap(~temperature, scales = "fixed")
> power.exponent.tfail.HRS.50.pct.plot

Error in log(x, base) : non-numeric argument to mathematical function


But my dataframe columns are well numeric: 

> is.numeric(power.exponent.tfail.HRS.50.pct.HRS$t.fail.yrs)
[1] TRUE
> class(power.exponent.tfail.HRS.50.pct.HRS$t.fail.yrs)
[1] "numeric"
> typeof(power.exponent.tfail.HRS.50.pct.HRS$t.fail.yrs)
[1] "double"

And Here's my session info : 

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252 LC_NUMERIC=C                   LC_TIME=French_France.1252    

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

other attached packages:
 [1] stringr_1.3.0       bindrcpp_0.2        lemon_0.4.1         extrafont_0.17      maptools_0.9-2      spdep_0.7-4         spData_0.2.7.4     
 [8] Matrix_1.2-7.1      sp_1.2-7            ade4_1.7-10         adespatial_0.1-1    segmented_0.5-3.0   viridis_0.5.0       viridisLite_0.3.0  
[15] ReporteRs_0.8.9     ReporteRsjars_0.0.3 gridExtra_2.3       ggplot2_2.2.1       pbapply_1.3-4       reshape2_1.4.3      scales_0.5.0       
[22] MASS_7.3-45         dplyr_0.7.4         naturalsort_0.1.3   xlsx_0.5.7          xlsxjars_0.6.1      rJava_0.9-9        

loaded via a namespace (and not attached):
 [1] nlme_3.1-128        bold_0.5.0          gmodels_2.16.2      RColorBrewer_1.1-2  progress_1.1.2      httr_1.3.1          tools_3.3.2        
 [8] vegan_2.4-6         R6_2.2.2            KernSmooth_2.23-15  mgcv_1.8-15         lazyeval_0.2.1      colorspace_1.3-2    permute_0.9-4      
[15] prettyunits_1.0.2   extrafontdb_1.0     curl_3.1            adephylo_1.1-11     expm_0.999-2        xml2_1.2.0          officer_0.2.1      
[22] labeling_0.3        digest_0.6.14       foreign_0.8-67      R.utils_2.6.0       base64enc_0.1-3     pkgconfig_2.0.1     htmltools_0.3.6    
[29] rvg_0.1.8           rlang_0.2.0         shiny_1.0.5         bindr_0.1           zoo_1.8-1           jsonlite_1.5        gtools_3.5.0       
[36] zip_1.0.0           R.oo_1.21.0         magrittr_1.5        Rcpp_0.12.15        munsell_0.4.3       ape_5.0             gdtools_0.1.7      
[43] R.methodsS3_1.7.1   stringi_1.1.6       plyr_1.8.4          adegraphics_1.0-9   gdata_2.18.0        parallel_3.3.2      adegenet_2.1.1     
[50] deldir_0.1-14       rncl_0.8.2          lattice_0.20-34     cowplot_0.9.2       splines_3.3.2       knitr_1.20          pillar_1.2.1       
[57] igraph_1.2.1        uuid_0.1-2          taxize_0.9.2        boot_1.3-18         seqinr_3.4-5        codetools_0.2-15    LearnBayes_2.15    
[64] crul_0.5.2          XML_3.98-1.10       glue_1.2.0          RNeXML_2.0.8        latticeExtra_0.6-28 data.table_1.10.4-3 png_0.1-7          
[71] httpuv_1.3.6.2      foreach_1.4.4       Rttf2pt1_1.3.6      gtable_0.2.0        purrr_0.2.4         tidyr_0.8.0         reshape_0.8.7      
[78] assertthat_0.2.0    mime_0.5            phylobase_0.8.4     xtable_1.8-2        coda_0.19-1         tibble_1.4.2        iterators_1.0.9    
[85] cluster_2.0.5 

Using ggplot2

Thanks in advance !!! 
Reply all
Reply to author
Forward
0 new messages