Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override ggplot2's axis formatting?

Tags:

r

ggplot2

When you choose a log scale, ggplot2 formats the breaks like 10^x. I'd like it to not do that. For example, the code below should display a graph with ticks at 1, 2, 5 etc, not 10^0, 10^0.3, 10^0.69 etc.

library(ggplot2)
dfr <- data.frame(x = 1:100, y = rlnorm(100))
breaks <- as.vector(c(1, 2, 5) %o% 10^(-1:1))
p1 <- ggplot(dfr, aes(x, y)) + geom_point() + scale_y_log10(breaks = breaks)
print(p1)

I guess that adding a formatter argument to scale_y_log10 would do the trick, but I'm not sure what to put in the argument, or where the options might be documented.

like image 689
Richie Cotton Avatar asked May 25 '10 17:05

Richie Cotton


People also ask

How do I change axis labels in R?

Changing axis labels To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

How do I stop Ggplot from scientific notation?

The scale_x_continuous() and scale_y_continuous() methods can be used to disable scientific notation and convert scientific labels to discrete form.

How do I increase the number of Y axis in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.


2 Answers

scale_y_log10(breaks = breaks, labels = breaks should do the trick.

like image 113
hadley Avatar answered Oct 22 '22 17:10

hadley


As of ggplot2 version 0.9.0, this behavior (formatting the axis labels as 10^0) is no longer the default. The code in the question now gives the desired result.

library(ggplot2)
dfr <- data.frame(x = 1:100, y = rlnorm(100))
breaks <- as.vector(c(1, 2, 5) %o% 10^(-1:1))
p1 <- ggplot(dfr, aes(x, y)) + geom_point() + scale_y_log10(breaks = breaks)
print(p1)

enter image description here

like image 4
Brian Diggs Avatar answered Oct 22 '22 17:10

Brian Diggs