1

I would like to get a R plot with log-scale axes and fixed equal breaks on x and y. I transform the axes into log scale using the following code.

library(ggplot2)
p <- ggplot(cars*1000, aes(x = speed, y = dist)) + geom_point() + 
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
  labels = trans_format("log10", math_format(10^.x))) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                   labels = trans_format("log10", math_format(10^.x))) 

which gives me this

Now I want the x and y limits and ticks to be exactly the same from 0 to 10^5. I tried p + coord_fixed() and p + coord_equal() but it does not give me the same scaling.

3
  • Set limits inside scale_*_log10 to be the same for both axes, then coord_fixed should work Commented Nov 26, 2019 at 20:54
  • Thanks! It doesn't seem to work. Just tried it in the following code and it returns an empty plot: ggplot(cars*1000, aes(x = speed, y = dist)) + geom_point() + scale_x_log10(limits=c(0,10^5),breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) + scale_y_log10(limits=c(0,10^5),breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x)))+geom_point() Commented Nov 26, 2019 at 21:16
  • Just changed limits to c(1,10^5) and it worked. Thanks! Commented Nov 26, 2019 at 21:21

1 Answer 1

1
library(ggplot2)
library(scales)
p <- ggplot(cars*1000, aes(x = speed, y = dist)) + geom_point() + 
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x), 
                labels = trans_format("log10", math_format(10^.x)), 
                limits = c(10^0, 10^5)) +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)),
                limits = c(10^0, 10^5)) 
p

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.