【问题标题】:How can I scale a plot without affecting the data that is fit?如何在不影响拟合数据的情况下缩放绘图?
【发布时间】:2021-11-19 13:50:19
【问题描述】:

我有一些数据有点拥挤,接近于零。因此,我想对轴使用对数刻度,以便更清楚地了解这些点。但是,当我这样做并使用非常方便的 ggpmisc::stat_poly_line 函数时,拟合是在缩放数据上完成的。我能以某种方式避免这种情况吗?

下面是我的意思的一个例子。

df <- structure(list(x = c(
  0.01, 0.763333333333333, 12.42, 0.243333333333333,
  49.4066666666667, 2.37333333333333, 1.08333333333333, 0.0533333333333333,
  0.01, 0.37, 0.01, NA
), y = c(
  0.00333333333333333, 0.183333333333333,
  19.7, 0.356666666666667, 38.6566666666667, 3.47, 1.12, 0.0666666666666667,
  0.01, 0.456666666666667, 0.0166666666666667, 0.00333333333333333
)), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))
ggplot2::ggplot(df, ggplot2::aes(x, y)) +
  ggplot2::geom_point() +
  ggpmisc::stat_poly_eq(
    formula = y ~ x + 0,
    mapping = ggplot2::aes(label = paste(ggplot2::after_stat(adj.rr.label)))
  ) +
  ggpmisc::stat_poly_line(formula = y ~ x + 0)
#> Warning: Removed 1 rows containing non-finite values (stat_poly_eq).
#> Warning: Removed 1 rows containing non-finite values (stat_smooth).
#> Warning: Removed 1 rows containing missing values (geom_point).

ggplot2::ggplot(df, ggplot2::aes(x, y)) +
  ggplot2::geom_point() +
  ggpmisc::stat_poly_eq(
    formula = y ~ x + 0,
    mapping = ggplot2::aes(label = paste(ggplot2::after_stat(adj.rr.label)))
  ) +
  ggpmisc::stat_poly_line(formula = y ~ x + 0) +
  ggplot2::scale_x_log10() +
  ggplot2::scale_y_log10()
#> Warning: Removed 1 rows containing non-finite values (stat_poly_eq).
#> Warning: Removed 1 rows containing non-finite values (stat_smooth).
#> Warning: Removed 1 rows containing missing values (geom_point).

reprex package (v2.0.1) 于 2021-09-27 创建

【问题讨论】:

    标签: r ggplot2 ggpmisc


    【解决方案1】:

    只要在stat_poly_eqaes里面做对数的逆运算

    ggplot2::ggplot(df, ggplot2::aes(x, y)) +
      ggplot2::geom_point() +
      ggpmisc::stat_poly_eq(
        formula = y ~ x + 0,
        mapping = ggplot2::aes(exp(x),exp(y),label = paste(ggplot2::after_stat(adj.rr.label)))
      ) +
      ggpmisc::stat_poly_line(formula = y ~ x + 0) +
      ggplot2::scale_x_log10() +
      ggplot2::scale_y_log10()
    

    【讨论】:

    • Doh,为解决方案喝彩 :) 我会稍等一下,以防出现任何其他答案,然后标记为已解决。
    猜你喜欢
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2013-07-24
    • 1970-01-01
    相关资源
    最近更新 更多