【问题标题】:Print Regression Coefficients on Graph (ggplot)在图形上打印回归系数 (ggplot)
【发布时间】:2017-01-13 22:54:28
【问题描述】:

我使用reg <- lm(...) 执行回归并获得一些我可以使用reg$coefficients 访问的系数。

它的类型为Named num,包含所有系数及其值。

Named num [1:11] 505.085 -0.251 -0.286 -0.22 -0.801 ...
- attr(*, "names")= chr [1:11] "(Intercept)" "year" "monthDez" "monthFeb" ...

我想在我用 ggplot 创建的图表上显示这些。我目前的做法是为此使用副标题:

labs(subtitle=paste(toString(names(reg$coefficients)), "\n",
     paste(reg$coefficients, collapse = "           ")))

但它没有正确对齐(名称直接覆盖值等) 有人有想法吗?

我现在的情节是这样的:

base <- ggplot(deliveries, aes(Date)) +
  geom_line(aes(y = SalesVolume, colour = "SalesVolume"))+
  ggtitle("Sales Volume By Time") +
  xlab("Time") +
  ylab("Sales Volume") +
  labs(subtitle=paste(toString(names(reg$coefficients)), "\n", paste(reg$coefficients, collapse = "           ")))

print(base + scale_x_date(labels = date_format("%b %y"), breaks =     date_breaks("2 months")))

在这个图表中显示了一个预测,所以我也想看看那里的回归系数。

【问题讨论】:

  • 如果没有构建示例图的代码,我很难想象您的需求。
  • @Benjamin:我已经添加了我的图表的缩短版本,本质是我需要一个带有回归系数的图表旁边的小表格/图例(但可能是任何其他命名的数)。
  • 一个不错的解决方案是使用ggpmisc,如this answer中所述

标签: r ggplot2


【解决方案1】:

是否可以制作两个单独的图并将它们排列到网格上?

library(ggplot2)
library(broom)
library(dplyr)
library(tidyr)

data_plot <- 
  ggplot(data = mtcars,
         mapping = aes(x = qsec,
                       y = mpg,
                       colour = factor(gear))) + 
  geom_point()

fit <- lm(mpg ~ qsec + wt + factor(gear), 
          data = mtcars)

# Make a data frame with the contents of the model.
reg_data <- 
  tidy(fit) %>%
  mutate(y = nrow(.):1 - 1) %>%
  gather(estimate, value,
         estimate:p.value) %>%
  mutate(estimate = factor(estimate,
                           c("term", "estimate", "std.error", 
                             "statistic", "p.value")))

# Make a plot displaying the table.
reg_plot <- 
  ggplot(data = reg_data,
         mapping = aes(x = estimate,
                       y = y)) + 
  geom_text(mapping = aes(label = round(value, 2))) + 
  scale_y_continuous(breaks = unique(reg_data[["y"]]),
                     labels = unique(reg_data[["term"]])) + 
  scale_x_discrete(position = "top") + 
  xlab("") + 
  ylab("") + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_blank())

# Arrange the two plots
gridExtra::grid.arrange(data_plot + theme(plot.margin = grid::unit(c(1,1,0,.5), "lines")), 
                        reg_plot + theme(plot.margin = grid::unit(c(0,0,1,0), "lines")), 
                        clip = FALSE, 
                        nrow = 2,
                        ncol = 1, 
                        heights = grid::unit(c(.70, .5),
                                             c("null", "null"))) 

【讨论】:

    【解决方案2】:

    根据我对 ggplot2 的有限经验,annotate() 可用于向使用 ggplot() 创建的绘图添加一些注释,但我不确定下面的代码是否适合您的需求

    reg <- lm(data = mtcars, mpg ~ wt)
    
    pred <- predict(reg)
    
    newdata <- data.frame(mtcars, pred)
    
    par <- summary(reg)$coefficients[,1]   # extract model parameters
    
    par.f <- format(par, digits = 2)       # set the decimal digits of parameters
    
    ggplot(mtcars, aes(x = wt, y = mpg)) + 
    
      geom_point() +
    
      geom_line(data = newdata, aes(x = wt, y = pred)) +
    
      annotate("text", x = c(2, 2.5), y = 18, label = names(reg$coefficients)) +
    
      annotate("text", x = c(2, 2.5), y = 16.5, label = par.f) # make them aligned by set x and y in annotate()
    

    enter image description here

    【讨论】:

    • 好主意,谢谢 :) 但我不知道 x 值和 y 值的范围(至少将它们设置为固定)。此外,x 值是日期值。
    猜你喜欢
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2018-02-24
    • 2019-03-18
    • 2013-03-16
    相关资源
    最近更新 更多