用对数 - 对数坐标绘制ggplot上的线性函数
问题描述:
我想用R(Ver 3.0.2)中的ggplot2(Ver 0.9.3.1)在对数坐标图上绘制三个函数。用对数 - 对数坐标绘制ggplot上的线性函数
y = x
y = 0.5*x
y = 1.5*x
我尝试过很多事情,但仍然遇到问题。这包括读入计算器问题here。
我想要的一个例子是here。我在Matlab中生成了这个图。
以下是我正在使用的代码示例,但目前它没有任何操作。最终,我想成为其他数据之上的一层(需要使用日志记录来显示结构)。
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
答
可以使用coord_trans
代替scale_.._log10
和coord_cartesian
喜欢的东西(使用annotation_logticks
的例子让适当标记的休息时间)
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)))
注意?annotation_logticks
提供这个问题的一些方法e
+0
太棒了!我把它放进来,然后添加我的其他代码,它正在工作......感谢您的反馈! –
为什么不使用'aes(x,y)',使用'aes(log(x,10),log(y,10))'? –