ggplot2 y-axis ticks not showing up on a log scale
Asked Answered
D

1

2

I am trying to use ggplot2 to create a boxplot graph but I am having trouble getting the ticks to show up as it does in the examples of the ggplot2 webiste.

Here is some fake data of tastiness of fruits:

apples <- data.frame(fruit=c(rep("apple", 30)), taste=runif(30, 30, 50)
banana <- data.frame(fruit=c(rep("banana", 30)), taste=runif(30, 300, 500))
orange <- data.frame(fruit=c(rep("orange", 30)), taste=runif(30, 3000, 5000))
fruits <- rbind(apples,banana,orange)

If I plot as in the ggplot2 website example the y-axis scale should looks something like:

enter image description here

Instead I get an axis like:

ggplot(fruits, aes(fruit, taste) ) +  geom_boxplot() + scale_y_log10()

enter image description here

How would I get the y-axis scale on scientific notation?

Dulles answered 11/3, 2012 at 2:28 Comment(0)
E
3

I'm assuming that you are using the new 0.9.0 version of ggplot2, which underwent a large amount of changes. This happens to be one of them, I believe.

If I recall correctly, enough people complained about the exponential format being the default, that this was changed. As per the transition guide you can achieve the same effect via:

library(ggplot2)
library(scales)
ggplot(fruits, aes(fruit, taste) ) +  
    geom_boxplot() + 
    scale_y_log10(breaks = trans_breaks('log10', function(x) 10^x),
                  labels = trans_format('log10', math_format(10^.x)))
Eley answered 11/3, 2012 at 3:14 Comment(2)
Doesn't seem to be working for me right now. I will have to play around with if for a while. It is not the scientific notation that is the most important part, it is the huge gap that is missing between 0 and 1000 on the y-axis. I must have updated to 0.8.9 by mistake when I updated my OS.Dulles
@Kev Glad it helped. I haven't quite wrapped my head around when scales needs to be explicitly loaded and when it doesn't yet either.Eley

© 2022 - 2024 — McMap. All rights reserved.