图形上的打印回归系数(ggplot)

时间:2017-01-13 10:55:00

标签: r ggplot2

我使用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")))

在此图表中显示预测,因此我也希望在那里看到回归系数。

2 个答案:

答案 0 :(得分: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"))) 

答案 1 :(得分:1)

根据我对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