A function for ggplot2: input strings for aes()
在我写文章画图时经常遇到的一个问题是:ggplot2 坐标轴的输入不支持输入数据框的变量名,通常会报错找不到对象
🌰:问题描述
data: early senate poll
library(tidyverse) # general tasks
library(broom) # tidy model output
library(ggthemes) # style the plots
poll_data <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/early-senate-polls/early-senate-polls.csv")
glimpse(poll_data)
## Observations: 107
## Variables: 4
## $ year <int> 2006, 2006, 2006, 2006, 2006, 2006, 2006...
## $ election_result <int> -39, -10, -9, -16, 40, 10, -2, -41, -31,...
## $ presidential_approval <int> 46, 33, 32, 33, 53, 44, 37, 39, 42, 33, ...
## $ poll_average <int> -28, -10, -1, -15, 39, 14, 2, -22, -27, ...
background: there is a strong correlation between polling numbers and the ultimate result of an election
构建模型: 线性模型
poll_lm <- lm(election_result ~ poll_average, data = poll_data)
summary(poll_lm)
##
## Call:
## lm(formula = election_result ~ poll_average, data = poll_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.4281 -5.0197 0.5601 6.1364 17.9357
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.89110 0.76969 -1.158 0.25
## poll_average 1.04460 0.03777 27.659 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.93 on 105 degrees of freedom
## Multiple R-squared: 0.8793, Adjusted R-squared: 0.8782
## F-statistic: 765 on 1 and 105 DF, p-value: < 2.2e-16
写个函数画出因变量和自变量的关系
结果出现了一个令我费解的报错
Error in FUN(X[[i]], …) : object ‘poll_average’ not found
我不断地检查我的拼写,直到我开始怀疑人生
解决办法:Define aesthetic mappings programatically
plot_model <- function(mod, explanatory, response, .fitted = ".fitted") {
augment(mod) %>%
ggplot() +
geom_point(aes_string(x = explanatory, y = response), color = "#2CA58D") +
geom_line(aes_string(x = explanatory, y = .fitted), color = "#033F63") +
theme_solarized() +
theme(axis.title = element_text()) +
labs(x = "Poll average", y = "Election results")
}
plot_model(poll_lm, "poll_average", "election_result")