【发布时间】: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中所述