|
Dear all,
I would like to (i) produce boxplot graphs with axis in logarithm in base 10 and (ii) showing the values on the axis in 10^exponent format rather than 10E+exponent. To illustrate with an example, I have some widely spread data that I chart plot using boxplot() [figure on the left]; the log="y" option of boxplot() I obtained the natural logarithm conversion of the data and the unfriendly notation baseE+exponent [figure on the centre]; if I log10 the data I obtain the desired plot, but the axis are showing only the exponent. [figure on the right]. Can anybody help? Best regards Luigi Marongiu, MSc ########### EXAMPLE ############ # generationg random numbers x<-runif(100, min=0, max=100000) # create plot par(mfrow = c(1,3)) #plotting in the left side boxplot(x, xlab="Linear values") #plotting in the centre boxplot(x, log = "y", xlab="y axis logged") # creating log10 values and plotting on the right side Log.base10.x<-log10(x) boxplot(Log.base10.x, xlab="LOG10 of data") [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
The key is to supply an expression, not text, to the labels argument to axis.
See help("plotmath") for details. Here is an example: x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) boxplot(x, log="y", yaxt="n") ylim <- par("usr")[3:4] log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, function(y)bquote(10^.(y))))) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of Luigi > Sent: Friday, June 22, 2012 7:54 AM > To: [hidden email] > Subject: [R] Boxplot with Log10 and base-exponent axis > > Dear all, > > I would like to (i) produce boxplot graphs with axis in logarithm in base 10 > and (ii) showing the values on the axis in 10^exponent format rather than > 10E+exponent. > > > > To illustrate with an example, I have some widely spread data that I chart > plot using boxplot() [figure on the left]; the log="y" option of boxplot() > I obtained the natural logarithm conversion of the data and the unfriendly > notation baseE+exponent [figure on the centre]; if I log10 the data I obtain > the desired plot, but the axis are showing only the exponent. [figure on the > right]. > > > > Can anybody help? > > > > Best regards > > Luigi Marongiu, MSc > > > > ########### EXAMPLE ############ > > # generationg random numbers > > x<-runif(100, min=0, max=100000) > > > > # create plot > > par(mfrow = c(1,3)) > > > > #plotting in the left side > > boxplot(x, xlab="Linear values") > > > > #plotting in the centre > > boxplot(x, log = "y", xlab="y axis logged") > > > > # creating log10 values and plotting on the right side > > Log.base10.x<-log10(x) > > boxplot(Log.base10.x, xlab="LOG10 of data") > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
You might prefer placing the tick marks with
log10AtY <- log10(axTicks(side=2)) log10AtY <- unique(round(log10AtY)) instead of the ylim <- par("usr")[3:4] log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) in my original example. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of William Dunlap > Sent: Friday, June 22, 2012 8:14 AM > To: Luigi; [hidden email] > Subject: Re: [R] Boxplot with Log10 and base-exponent axis > > The key is to supply an expression, not text, to the labels argument to axis. > See help("plotmath") for details. Here is an example: > x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) > boxplot(x, log="y", yaxt="n") > ylim <- par("usr")[3:4] > log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) > axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, > function(y)bquote(10^.(y))))) > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: [hidden email] [mailto:[hidden email]] On Behalf > > Of Luigi > > Sent: Friday, June 22, 2012 7:54 AM > > To: [hidden email] > > Subject: [R] Boxplot with Log10 and base-exponent axis > > > > Dear all, > > > > I would like to (i) produce boxplot graphs with axis in logarithm in base 10 > > and (ii) showing the values on the axis in 10^exponent format rather than > > 10E+exponent. > > > > > > > > To illustrate with an example, I have some widely spread data that I chart > > plot using boxplot() [figure on the left]; the log="y" option of boxplot() > > I obtained the natural logarithm conversion of the data and the unfriendly > > notation baseE+exponent [figure on the centre]; if I log10 the data I obtain > > the desired plot, but the axis are showing only the exponent. [figure on the > > right]. > > > > > > > > Can anybody help? > > > > > > > > Best regards > > > > Luigi Marongiu, MSc > > > > > > > > ########### EXAMPLE ############ > > > > # generationg random numbers > > > > x<-runif(100, min=0, max=100000) > > > > > > > > # create plot > > > > par(mfrow = c(1,3)) > > > > > > > > #plotting in the left side > > > > boxplot(x, xlab="Linear values") > > > > > > > > #plotting in the centre > > > > boxplot(x, log = "y", xlab="y axis logged") > > > > > > > > # creating log10 values and plotting on the right side > > > > Log.base10.x<-log10(x) > > > > boxplot(Log.base10.x, xlab="LOG10 of data") > > > > > > > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by William Dunlap
> The key is to supply an expression, not text, to the labels argument to axis. > See help("plotmath") for details. Here is an example: > x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) > boxplot(x, log="y", yaxt="n") > ylim <- par("usr")[3:4] > log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) > axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, function(y)bquote(10^.(y))))) Yes, that's nice, and exactly the basic idea. For a few years now, the eaxis() function in package "sfsmisc" does (something like) this (and a bit more) even more nicely : install.packages("sfsmisc") require("sfsmisc") x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) boxplot(x, log="y", yaxt="n") eaxis(2) -- Martin Maechler, ETH Zurich > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > -----Original Message----- > > From: [hidden email] [mailto:[hidden email]] On Behalf > > Of Luigi > > Sent: Friday, June 22, 2012 7:54 AM > > To: [hidden email] > > Subject: [R] Boxplot with Log10 and base-exponent axis > > > > Dear all, > > > > I would like to (i) produce boxplot graphs with axis in logarithm in base 10 > > and (ii) showing the values on the axis in 10^exponent format rather than > > 10E+exponent. > > > > > > > > To illustrate with an example, I have some widely spread data that I chart > > plot using boxplot() [figure on the left]; the log="y" option of boxplot() > > I obtained the natural logarithm conversion of the data and the unfriendly > > notation baseE+exponent [figure on the centre]; if I log10 the data I obtain > > the desired plot, but the axis are showing only the exponent. [figure on the > > right]. > > > > > > > > Can anybody help? > > > > > > > > Best regards > > > > Luigi Marongiu, MSc > > > > > > > > ########### EXAMPLE ############ > > > > # generationg random numbers > > > > x<-runif(100, min=0, max=100000) > > > > > > > > # create plot > > > > par(mfrow = c(1,3)) > > > > > > > > #plotting in the left side > > > > boxplot(x, xlab="Linear values") > > > > > > > > #plotting in the centre > > > > boxplot(x, log = "y", xlab="y axis logged") > > > > > > > > # creating log10 values and plotting on the right side > > > > Log.base10.x<-log10(x) > > > > boxplot(Log.base10.x, xlab="LOG10 of data") > > > > > > > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by William Dunlap
Thank you!
This works good. I understand that the value are now in Log10 scale, although I did not understand what is happening at line 4 of your script. I would like to ask how can I change the y limits since they now depend on par("user"). What if I'd like y limits extending from 1000 to 1 000 000? I've tried to modify ylim (line 3) and ceiling (line 4) but I obtained errors. Best wishes, Luigi ############### UPDATED EXAMPLE ############################################ # generationg random numbers x<-runif(100, min=0, max=100000) #plotting boxplot(x, log = "y", yaxt="n") ylim <- par("usr")[3:4] log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, function(y)bquote(10^.(y))))) ############################################################################ # ------------------ Your Response -------------------------------------------- The key is to supply an expression, not text, to the labels argument to axis. See help("plotmath") for details. Here is an example: 1 x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) 2 boxplot(x, log="y", yaxt="n") 3 ylim <- par("usr")[3:4] 4 log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) 5 axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, function(y)bquote(10^.(y))))) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of Luigi > Sent: Friday, June 22, 2012 7:54 AM > To: [hidden email] > Subject: [R] Boxplot with Log10 and base-exponent axis > > Dear all, > > I would like to (i) produce boxplot graphs with axis in logarithm in base 10 > and (ii) showing the values on the axis in 10^exponent format rather than > 10E+exponent. > > > > To illustrate with an example, I have some widely spread data that I chart > plot using boxplot() [figure on the left]; the log="y" option of boxplot() > I obtained the natural logarithm conversion of the data and the unfriendly > notation baseE+exponent [figure on the centre]; if I log10 the data I obtain > the desired plot, but the axis are showing only the exponent. [figure on the > right]. > > > > Can anybody help? > > > > Best regards > > Luigi Marongiu, MSc > > > > ########### EXAMPLE ############ > > # generationg random numbers > > x<-runif(100, min=0, max=100000) > > > > # create plot > > par(mfrow = c(1,3)) > > > > #plotting in the left side > > boxplot(x, xlab="Linear values") > > > > #plotting in the centre > > boxplot(x, log = "y", xlab="y axis logged") > > > > # creating log10 values and plotting on the right side > > Log.base10.x<-log10(x) > > boxplot(Log.base10.x, xlab="LOG10 of data") > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
To change the y-limits you need to add ylim=c(min,max)
to the call to boxplot. The limits there should be in the units of the y variable (not its log10). par("usr")[3:4] report the y-limits on the log10 scale (they will be expanded a bit from your desired limits so the plot does not extend all the way to edge). > boxplot(abs(tan(1:100)), log="y", ylim=c(.001, 1000)) > par("usr")[3:4] [1] -3.24 3.24 > 10 ^ par("usr")[3:4] [1] 5.754399e-04 1.737801e+03 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: Luigi [mailto:[hidden email]] > Sent: Saturday, June 23, 2012 8:49 AM > To: William Dunlap > Cc: 'Martin Maechler'; [hidden email] > Subject: RE: [R] Boxplot with Log10 and base-exponent axis > > Thank you! > This works good. I understand that the value are now in Log10 scale, > although I did not understand what is happening at line 4 of your script. > I would like to ask how can I change the y limits since they now depend on > par("user"). What if I'd like y limits extending from 1000 to 1 000 000? > I've tried to modify ylim (line 3) and ceiling (line 4) but I obtained > errors. > Best wishes, > Luigi > > > ############### UPDATED EXAMPLE ############################################ > # generationg random numbers > x<-runif(100, min=0, max=100000) > > > #plotting > boxplot(x, log = "y", yaxt="n") > ylim <- par("usr")[3:4] > > log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) > > axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, > function(y)bquote(10^.(y))))) > > ############################################################################ > # > > > ------------------ Your Response > -------------------------------------------- > The key is to supply an expression, not text, to the labels argument to > axis. > See help("plotmath") for details. Here is an example: > 1 x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) > 2 boxplot(x, log="y", yaxt="n") > 3 ylim <- par("usr")[3:4] > 4 log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) > 5 axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, > function(y)bquote(10^.(y))))) > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: [hidden email] [mailto:[hidden email]] > On Behalf > > Of Luigi > > Sent: Friday, June 22, 2012 7:54 AM > > To: [hidden email] > > Subject: [R] Boxplot with Log10 and base-exponent axis > > > > Dear all, > > > > I would like to (i) produce boxplot graphs with axis in logarithm in base > 10 > > and (ii) showing the values on the axis in 10^exponent format rather than > > 10E+exponent. > > > > > > > > To illustrate with an example, I have some widely spread data that I chart > > plot using boxplot() [figure on the left]; the log="y" option of > boxplot() > > I obtained the natural logarithm conversion of the data and the unfriendly > > notation baseE+exponent [figure on the centre]; if I log10 the data I > obtain > > the desired plot, but the axis are showing only the exponent. [figure on > the > > right]. > > > > > > > > Can anybody help? > > > > > > > > Best regards > > > > Luigi Marongiu, MSc > > > > > > > > ########### EXAMPLE ############ > > > > # generationg random numbers > > > > x<-runif(100, min=0, max=100000) > > > > > > > > # create plot > > > > par(mfrow = c(1,3)) > > > > > > > > #plotting in the left side > > > > boxplot(x, xlab="Linear values") > > > > > > > > #plotting in the centre > > > > boxplot(x, log = "y", xlab="y axis logged") > > > > > > > > # creating log10 values and plotting on the right side > > > > Log.base10.x<-log10(x) > > > > boxplot(Log.base10.x, xlab="LOG10 of data") > > > > > > > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by Martin Maechler
This (and William's solution) is so good, I must ask:
Is there a good reason why this is not the default functionality in the graphics package? The default displays the number 1 as 1+e00 which is hideous! -----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Martin Maechler Sent: Saturday, 23 June 2012 4:57a To: Luigi; William Dunlap Cc: [hidden email] Subject: Re: [R] Boxplot with Log10 and base-exponent axis > The key is to supply an expression, not text, to the labels argument to axis. > See help("plotmath") for details. Here is an example: > x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) > boxplot(x, log="y", yaxt="n") > ylim <- par("usr")[3:4] > log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) > axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, function(y)bquote(10^.(y))))) Yes, that's nice, and exactly the basic idea. For a few years now, the eaxis() function in package "sfsmisc" does (something like) this (and a bit more) even more nicely : install.packages("sfsmisc") require("sfsmisc") x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) boxplot(x, log="y", yaxt="n") eaxis(2) -- Martin Maechler, ETH Zurich ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by William Dunlap
Dear Mr Dunlap,
Your solution works really fine. Thank you for your time, Best wishes, Luigi -----Original Message----- From: William Dunlap [mailto:[hidden email]] Sent: 23 June 2012 18:48 To: Luigi Cc: 'Martin Maechler'; [hidden email] Subject: RE: [R] Boxplot with Log10 and base-exponent axis To change the y-limits you need to add ylim=c(min,max) to the call to boxplot. The limits there should be in the units of the y variable (not its log10). par("usr")[3:4] report the y-limits on the log10 scale (they will be expanded a bit from your desired limits so the plot does not extend all the way to edge). > boxplot(abs(tan(1:100)), log="y", ylim=c(.001, 1000)) > par("usr")[3:4] [1] -3.24 3.24 > 10 ^ par("usr")[3:4] [1] 5.754399e-04 1.737801e+03 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: Luigi [mailto:[hidden email]] > Sent: Saturday, June 23, 2012 8:49 AM > To: William Dunlap > Cc: 'Martin Maechler'; [hidden email] > Subject: RE: [R] Boxplot with Log10 and base-exponent axis > > Thank you! > This works good. I understand that the value are now in Log10 scale, > although I did not understand what is happening at line 4 of your script. > I would like to ask how can I change the y limits since they now depend on > par("user"). What if I'd like y limits extending from 1000 to 1 000 000? > I've tried to modify ylim (line 3) and ceiling (line 4) but I obtained > errors. > Best wishes, > Luigi > > > ############### UPDATED EXAMPLE > # generationg random numbers > x<-runif(100, min=0, max=100000) > > > #plotting > boxplot(x, log = "y", yaxt="n") > ylim <- par("usr")[3:4] > > log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) > > axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, > function(y)bquote(10^.(y))))) > > > # > > > ------------------ Your Response > -------------------------------------------- > The key is to supply an expression, not text, to the labels argument to > axis. > See help("plotmath") for details. Here is an example: > 1 x <- list(One=10^(sin(1:10)+5), Two=10^(cos(1:30)*2)) > 2 boxplot(x, log="y", yaxt="n") > 3 ylim <- par("usr")[3:4] > 4 log10AtY <- seq(ceiling(ylim[1]), floor(ylim[2])) > 5 axis(side=2, at=10^log10AtY, lab=as.expression(lapply(log10AtY, > function(y)bquote(10^.(y))))) > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: [hidden email] [mailto:[hidden email]] > On Behalf > > Of Luigi > > Sent: Friday, June 22, 2012 7:54 AM > > To: [hidden email] > > Subject: [R] Boxplot with Log10 and base-exponent axis > > > > Dear all, > > > > I would like to (i) produce boxplot graphs with axis in logarithm in > 10 > > and (ii) showing the values on the axis in 10^exponent format rather than > > 10E+exponent. > > > > > > > > To illustrate with an example, I have some widely spread data that I chart > > plot using boxplot() [figure on the left]; the log="y" option of > boxplot() > > I obtained the natural logarithm conversion of the data and the unfriendly > > notation baseE+exponent [figure on the centre]; if I log10 the data I > obtain > > the desired plot, but the axis are showing only the exponent. [figure on > the > > right]. > > > > > > > > Can anybody help? > > > > > > > > Best regards > > > > Luigi Marongiu, MSc > > > > > > > > ########### EXAMPLE ############ > > > > # generationg random numbers > > > > x<-runif(100, min=0, max=100000) > > > > > > > > # create plot > > > > par(mfrow = c(1,3)) > > > > > > > > #plotting in the left side > > > > boxplot(x, xlab="Linear values") > > > > > > > > #plotting in the centre > > > > boxplot(x, log = "y", xlab="y axis logged") > > > > > > > > # creating log10 values and plotting on the right side > > > > Log.base10.x<-log10(x) > > > > boxplot(Log.base10.x, xlab="LOG10 of data") > > > > > > > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
| Powered by Nabble | Edit this page |
