Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert to from scientific notation to decimal with ggplot

I am trying to plot some test data (show at the end of this post), using ggplot using the following command:

options(scipen=20)    
ggplot(testData,aes(x=Group.1, y=x), format(scientific=FALSE)) + 
scale_x_discrete(name="Test", formatter=comma) + geom_bar()

The plot generates well enough, but the x-axis is in scientific notation (as in the test data), how would i go about displaying it at decimal instead? (As shown in the command i already tried the scipen option and various formatter options - as suggested in other posts on this site)

My test data:

> str(testData)
'data.frame':   5 obs. of  2 variables:
$ Group.1: Factor w/ 5 levels "(1.3e+03,1.5e+03]",..: 1 2 3 4 5
$ x      : num  80000 94000 86000 112000 98000

> testData
            Group.1      x
1 (1.3e+03,1.5e+03]  80000
2 (1.5e+03,1.7e+03]  94000
3 (1.7e+03,1.9e+03]  86000
4 (1.9e+03,2.1e+03] 112000
5 (2.1e+03,2.3e+03]  98000

> dput(testData)
structure(list(Group.1 = structure(1:5, .Label = c("(1.3e+03,1.5e+03]", 
"(1.5e+03,1.7e+03]", "(1.7e+03,1.9e+03]", "(1.9e+03,2.1e+03]", 
"(2.1e+03,2.3e+03]"), class = "factor"), x = c(80000, 94000, 
86000, 112000, 98000)), .Names = c("Group.1", "x"), row.names = c(NA, 
-5L), class = "data.frame")

The generation of testData:

testData <- aggregate(as.numeric(as.character(housing7$PRICE)),
                      by=list(cut(as.numeric(as.character(housing7$SIZE)), 
                      breaks=5)), FUN=mean)
like image 954
Johnny Avatar asked Jun 29 '12 22:06

Johnny


1 Answers

Once the labels of your factor are stored in scientific notation, it isn't easy to go back to normal notation.

Since you used cut to create this factor, you can change the number of digits used in the label with the dig.lab argument. For example:

testData <- aggregate(as.numeric(as.character(housing7$PRICE)),
by=list(cut(as.numeric(as.character(housing7$SIZE)), breaks=5,dig.lab=6)), FUN=mean)
like image 76
nograpes Avatar answered Oct 11 '22 23:10

nograpes