带有固定对数轴的ggplot2散点图

时间:2019-11-26 20:43:42

标签: r ggplot2

我想获得一个带有对数刻度轴并在x和y上固定等距的R图。我使用以下代码将轴转换为对数刻度。

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

现在,我希望x和y的限制和刻度在0到10 ^ 5之间完全相同。我尝试过p + coord_fixed()p + coord_equal(),但是缩放比例却不一样。

1 个答案:

答案 0 :(得分:0)

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

Issue page of Github