使用log-log标度在ggplot上绘制线性函数

Jus*_*ter 7 logging r scale ggplot2

我想在R(Ver 3.0.2)中使用ggplot2(Ver 0.9.3.1)在对数 - 对数图上绘制三个函数.

y = x
y = 0.5*x
y = 1.5*x
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多东西,但一直遇到问题.这包括在此处阅读stackoverflow问题.

我希望拥有的一个例子就在这里.我在Matlab中生成了该绘图.

以下是我正在使用的代码示例,但目前它没有任何问题.最终,我希望成为其他数据之上的一个层(需要log-log来显示结构).

library(ggplot2)

plot = ggplot()
plot = plot + coord_cartesian(xlim = c(0.02, 300), ylim = c(0.035, 20))
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {x}, aes(x,y), geom = "line", color = "blue")
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {0.5*x}, aes(x,y), geom = "line", color = "red")
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {1.5*x}, aes(x,y), geom = "line", color = "red")
plot = plot + scale_x_log10() + scale_y_log10() + annotation_logticks()
plot  
Run Code Online (Sandbox Code Playgroud)

mne*_*nel 11

You can use coord_trans instead of scale_.._log10 and coord_cartesian

Something like (using the examples from annotation_logticks to get the breaks labelled appropriately)

ggplot(data =  data.frame(x=c(0.0001,1000), y=c(0.001,1)), aes(x=x,y=y)) +
   stat_function(fun = function(x) x, geom='line',colour ='blue') +
   stat_function(fun = function(x) 0.5*x, geom='line',colour = 'red') +
   stat_function(fun = function(x) 1.5 * x , geom = 'line', colour = 'red') +
   coord_trans(xtrans = 'log10',ytrans = 'log10', limx = c(0.02,300), limy =c(0.035,20)) +
  annotation_logticks(scaled=FALSE) +
   scale_x_continuous(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) +
  scale_y_continuous(breaks = trans_breaks("log10", function(x) 10^x),
                     labels = trans_format("log10", math_format(10^.x)))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Note ?annotation_logticks provide a number of approaches to this issue