gpt4 book ai didi

r - 使用双对数刻度在 ggplot 上绘制线性函数

转载 作者:行者123 更新时间:2023-12-02 08:13:58 27 4
gpt4 key购买 nike

我想使用 R(版本 3.0.2)中的 ggplot2(版本 0.9.3.1)在双对数图上绘制三个函数。

y = x
y = 0.5*x
y = 1.5*x

我已经尝试了很多方法,但仍然遇到问题。这包括阅读 stackoverflow 问题 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_.._log10coord_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)))

enter image description here

注意?annotation_logticks提供了解决此问题的多种方法

关于r - 使用双对数刻度在 ggplot 上绘制线性函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19238360/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com