【问题标题】:Adding a general abline in log-log ggplot2在 log-log ggplot2 中添加一般 abline
【发布时间】:2014-06-18 14:15:53
【问题描述】:

我正在尝试添加一行来分隔 ggplot2 中的部分数据。按照这个线程: Adding linear model abline to log-log plot in ggplot

我试过了

d = data.frame(x = 100*rlnorm(100), y = 100*rlnorm(100))
ggplot(d, aes(x, y)) + geom_point() + 
  geom_abline(intercept = 100, slope = -1, col='red') +
  scale_y_log10() + scale_x_log10()

但它没有画线。请注意,旧的情节方法没有问题:

plot(d$x, d$y, log='xy')
abline(a = 100, b=-1, col='red', untf=TRUE)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这可能不是最优雅的解决方案,但当我将预测添加到绘图时,我通常会为预测定义一个单独的数据框。我知道在很多方面将模型规范添加为绘图的一部分会更快,但我真的很喜欢将它作为一个单独的对象的灵活性。这是我在这种情况下的想法:

    d = data.frame(x = 100*rlnorm(100), y = 100*rlnorm(100))
    
    p = ggplot(d, aes(x,y)) + geom_point() + scale_x_log10() + scale_y_log10()
    
    pred.func = function(x){
      100 - x
    }
    
    new.dat = data.frame(x = seq(from = 5, to = 90))
    new.dat$pred = pred.func(new.dat$x)
    
    p + geom_line(aes(x = x, y = pred), data = new.dat, col = "red")
    

    【讨论】:

    • 我实际上认为geom_abline 根本没有与untrf 相同的功能(即untrf 参数)abline,并且只会在当前坐标上绘制直线,所以这个可能是最好的方法。
    猜你喜欢
    • 2021-09-25
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    相关资源
    最近更新 更多