Forcing 1e3 instead of 1000 in ggplot R

I'm having problems with y formatting ranges. When I use scale_y_log10() in my plot, he decides that using a scale of 0.1, 10, 1000 is the way to do this. I really need to display it as 1e-1, 1e1, 1e3 . math_format man page is not needed without the format I need to know.

All that I can answer will be.

+1
r ggplot2
03 Sep '13 at 19:34
source share
3 answers

You can use the breaks and labels scale_y_log10 , as in

 library(ggplot2) ggplot(data=subset(movies, votes > 1000)) + aes(x = rating, y = votes / 10000) + scale_y_log10(breaks = c(0.1, 1, 10), labels = expression(10^-1, 10^0, 10^1)) + geom_point() 

This may not be an elegant solution, but it works if you have only a limited number of schedules.

+2
Sep 03 '13 at 19:53
source share

The problem is that R uses a poorly understood punishment mechanism to decide whether to print in regular or scientific notation. This is determined by options( scipen ) .

The value represents the penalty R applies to the number of characters that it would have to print in scientific notation compared to a fixed point, so options( scipen = 3 ) means that R adds 3 to the number of characters that are required for printing. 1e2 and compares it with the number of characters it needs to print the fixed-point equivalent, and prints a number with fewer characters (so in this case 1e2 = 3 characters + 3 penalties = 6, while 100 is 3 characters, so 100 is printed To fix your example, just set options( scipen = -10 ) to always suggest printing scientific notation at a fixed point, so (using the @PeterB example) you can use scipen , which should let you not worry about setting a manual break .. .

 option( scipen = -10 ) ggplot(data=subset(movies, votes > 1000)) + aes(x = rating, y = votes / 10000) + geom_point() 

enter image description here

+6
Sep 03 '13 at 20:14
source share

The easiest way to achieve what you ask for, with automatic limits and interruptions and no side effects:

 library(ggplot2) library(MASS) library(scales) ggplot(data=subset(movies, votes > 1000)) + aes(x = rating, y = votes / 10000) + scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x, n=3), labels = trans_format("log10")) + geom_point() 

I prefer to use superscripts for powers of ten, and hide the minor grid, and add ticks located at a distance according to the logs. It is also quite easy to achieve:

 ggplot(data=subset(movies, votes > 1000)) + aes(x = rating, y = votes / 10000) + scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x, n=3), labels = trans_format("log10", math_format(10^.x))) + theme(panel.grid.minor = element_blank()) + annotation_logticks(sides="l") + geom_point() 

The above code is in the help examples annotation_logticks, annotation_logticks . There is a lot of flexibility for setting the exact format.

+2
Sep 04 '13 at 8:21
source share



All Articles