【问题标题】:geom_abline multiple slopes and interceptsgeom_abline 多个斜率和截距
【发布时间】:2018-01-19 08:22:28
【问题描述】:

考虑这个初始数据帧(yld_sum):

  coef     pred      se    ci.lb    ci.ub    cr.lb    cr.ub Yld_class
   b0 3164.226 114.256 2940.289 3388.164 2142.724 4185.728      1Low
   b1  -20.698   3.511  -27.580  -13.816  -50.520    9.124      1Low
   b0 3985.287 133.220 3724.180 4246.394 2954.998 5015.576      2Low
   b1  -14.371   4.185  -22.573   -6.168  -44.525   15.784      2Low

如何简化语法以绘制两条估计的回归线及其各自的 CI,并获得以下图?

这是我的详细代码:

library(tidyverse)

yld_sum_est <- yld_sum %>% select(Yld_class, coef, pred) %>% 
  spread(coef, pred)  

yld_sum_low <- yld_sum %>% select(Yld_class, coef, ci.lb) %>% 
  spread(coef, ci.lb)

yld_sum_up <- yld_sum %>% select(Yld_class, coef, ci.ub) %>% 
  spread(coef, ci.ub)

ggplot() + 
  geom_abline(data = yld_sum_est, aes(intercept = b0, slope = b1)) +
  geom_abline(data = yld_sum_low, aes(intercept = b0, slope = b1), linetype= "dashed") +
  geom_abline(data = yld_sum_up, aes(intercept = b0, slope = b1), linetype= "dashed") +
  scale_x_continuous(limits=c(0,60), name="x") +
  scale_y_continuous(limits=c(1000, 4200), name="y") 

【问题讨论】:

  • 您能更具体地谈谈您的不满吗?您对数据转换代码不满意吗?绘图代码?通用性和可扩展性?
  • 我想使用初始表来绘制该图...我想避免中间数据框...
  • 好吧,你不能直接从那个表中很好地绘图。我建议的简化方法是将您的数据框组合成一个单独的列,指示估计值、ub 或 lb,然后您可以使用 linetype = ifelse(type == "estimate"), group = interaction(type, Yld_class) 美学做一个 geom_abline。我看不出你的转换代码如何以该表为开始变得更简单,但也许从模型开始会更容易......
  • 我将更新 OP Gregor 解决您的评论..
  • @Gregor 我在这里创建了一个新问题来解决您的评论:stackoverflow.com/questions/45700172/…

标签: r ggplot2 tidyverse


【解决方案1】:

这是一个“数据形状”问题。如果您希望ggplot 在一次调用中绘制多个对象,对象参数(如interceptslope)需要是数据框的列,对象实例是数据时的行输入ggplot

特别是在您的情况下,您需要一个包含 6 行的数据框 - 每行一个,每行包含该行的标识及其参数,如下所示:

library(tidyverse)

# data block from the question is in clipboard
read.table("clipboard", header = T) -> yld_sum

yld_sum %>%
  select(-starts_with("cr"), -se) %>%
  gather(metric, value, -coef, -Yld_class) %>%
  spread(coef, value) %>% 
  ggplot() +
  geom_abline(aes(
    intercept = b0, 
    slope = b1,
    linetype = if_else(metric == "pred", "", "dashed")),
    ) +
  scale_x_continuous(limits=c(0,60), name="x") +
  scale_y_continuous(limits=c(1000, 4200), name="y") +
  guides(linetype = F)

通过将 RStudio 的 View 调用置于连续步骤之后(如 %&gt;% View),随意探索数据重塑过程。

数据的最终形状,用于说明(在spread() 调用之后):

  Yld_class metric       b0      b1
1      1Low  ci.lb 2940.289 -27.580
2      1Low  ci.ub 3388.164 -13.816
3      1Low   pred 3164.226 -20.698
4      2Low  ci.lb 3724.180 -22.573
5      2Low  ci.ub 4246.394  -6.168
6      2Low   pred 3985.287 -14.371

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2020-10-16
    相关资源
    最近更新 更多