Matthew Dubins
2007-Sep-23 17:50 UTC
[R] Plotting numbers at a specified decimal length on a plot()
Hi there, I want to figure out how to plot means, with 2 decimal places, of any Y variable on a scatterplot according to any X variable (which obviously should have limited scope). I already figured out how to plot the means, but without limiting their precision to 2 decimal places. This is the code I used once I had the scatterplot drawn: text(c(1990, 1992, 1994, 1996, 1998, 1999, 2000, 2001, 2002, 2003), 25, mean.yrly.closures$values, cex=.7) It's a plot of school closures by Year. The closures variable is labeled "values" in the "mean.yrly.closures" data set. I managed to *display* the mean.yrly.closures data set with a precision of 2 decimal places (options(digits=2)), but then once I plotted the numbers, they showed up with many decimal places. It would be nice to figure this out so that I don't have to rely on Excel to make changes to the precision! Thanks, Matthew Dubins
P Ehlers
2007-Sep-24 02:16 UTC
[R] Plotting numbers at a specified decimal length on a plot()
I find sprintf() useful for this. Compare lab <- rnorm(8) plot(1:10) text(2:9, 2:9, lab) with lab2 <- sprintf("%4.2f", lab) plot(1:10) text(2:9, 2:9, lab2) - Peter Ehlers Matthew Dubins wrote:> Hi there, > > I want to figure out how to plot means, with 2 decimal places, of any Y > variable on a scatterplot according to any X variable (which obviously > should have limited scope). I already figured out how to plot the > means, but without limiting their precision to 2 decimal places. This > is the code I used once I had the scatterplot drawn: > > text(c(1990, 1992, 1994, 1996, 1998, 1999, 2000, 2001, 2002, 2003), 25, > mean.yrly.closures$values, cex=.7) > > > It's a plot of school closures by Year. The closures variable is > labeled "values" in the "mean.yrly.closures" data set. I managed to > *display* the mean.yrly.closures data set with a precision of 2 decimal > places (options(digits=2)), but then once I plotted the numbers, they > showed up with many decimal places. > > It would be nice to figure this out so that I don't have to rely on > Excel to make changes to the precision! > > Thanks, > Matthew Dubins > > ______________________________________________ > R-help at r-project.org 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. > >
Marc Schwartz
2007-Sep-24 02:24 UTC
[R] Plotting numbers at a specified decimal length on a plot()
On Sun, 2007-09-23 at 13:50 -0400, Matthew Dubins wrote:> Hi there, > > I want to figure out how to plot means, with 2 decimal places, of any Y > variable on a scatterplot according to any X variable (which obviously > should have limited scope). I already figured out how to plot the > means, but without limiting their precision to 2 decimal places. This > is the code I used once I had the scatterplot drawn: > > text(c(1990, 1992, 1994, 1996, 1998, 1999, 2000, 2001, 2002, 2003), 25, > mean.yrly.closures$values, cex=.7) > > > It's a plot of school closures by Year. The closures variable is > labeled "values" in the "mean.yrly.closures" data set. I managed to > *display* the mean.yrly.closures data set with a precision of 2 decimal > places (options(digits=2)), but then once I plotted the numbers, they > showed up with many decimal places. > > It would be nice to figure this out so that I don't have to rely on > Excel to make changes to the precision! > > Thanks, > Matthew DubinsR uses double precision floats internally to store numeric values by default. See ?numeric What you see displayed when a number is print()ed, is different by default than the actual storage precision. This is generally controlled by the use of options("digits"). However, note that options("digits") does NOT control the number of digits to the right of the decimal place, but the number of SIGNIFICANT digits displayed. Also note that is it only a suggestion and not absolute. In addition, when printing multiple values, such as a vector, other characteristics will be in play. See ?print.default for more details here. To predictably control the number of digits to the right of the decimal when displaying numbers, either in textual output or in a plot, you need to format the output using functions such as sprintf() or formatC(), the former being preferred. See ?sprintf and ?formatC for more information. HTH, Marc Schwartz
Reasonably Related Threads
- R routines vs. MATLAB/SPSS Routines
- Getting group-wise standard scores of a vector
- Random sample from a data frame where ID column values don't match the values in an ID column in a second data frame
- New to R - Errors in plotting
- [LLVMdev] Implementing closures and continuations