Skip to content

Instantly share code, notes, and snippets.

@chapkovski
Created January 15, 2021 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chapkovski/e4d590b690739d237acf9d1ecd8e3d49 to your computer and use it in GitHub Desktop.
Save chapkovski/e4d590b690739d237acf9d1ecd8e3d49 to your computer and use it in GitHub Desktop.
plot for impeachment year - weight
library('pacman')
p_load('dplyr', 'estimatr', 'tidyverse', 'readxl', 'sjPlot', 'emmeans', 'olsrr', 'writexl', 'gtsummary', 'stargazer', 'magrittr', 'ggplot2', 'scales','lemon','knitr','ggpmisc', 'fastDummies','kableExtra','wesanderson','viridis','Hmisc','ggpubr','broom','purrr','car','rstatix', 'modelsummary', 'latex2exp', 'pastecs','skimr','haven', 'hrbrthemes','kableExtra', 'skimr','lubridate', 'data.table','ggthemes','summarytools','ggsci','gt','plm','png', 'RCurl', 'jpeg','grid','ggtext')
# forecasts are made using linear regression
#
df<- data.frame(id_=c(1,2,3,4),year=c(1868,
1998,
2019,
2021),
weight=c(79, 102, 131, 131)
)
model<- lm(year~id_, data=df)
model2<- lm(weight~id_, data=df)
next_ev <-data.frame(
id_ = c(4),
year=c(2048)
)
# To predict year:
predict(model, newdata = next_ev)
# To predict weight
predict(model2, newdata = next_ev)
# Updated data frame with predicted values
df<- data.frame(id_=c(1,2,3,4,5),year=c(1868,
1998,
2019,
2021,
2048),
weight=c(79, 102, 131, 131, 138),
lab=c('a','b','c','d','forecast'))
johnson_img<- readPNG('~/downloads/johnson.png')
clinton_img <- readPNG('~/downloads/clinton.png')
trump_img <- readPNG('~/downloads/trump.png')
forecast_img <- readPNG('~/downloads/forecast.png')
johnson_grab<- rasterGrob(johnson_img, interpolate=TRUE)
clinton_grab<- rasterGrob(clinton_img, interpolate=TRUE)
trump_grab<- rasterGrob(trump_img, interpolate=TRUE)
forecast_grab<- rasterGrob(forecast_img, interpolate=TRUE)
df %>% ggplot(aes(x=year, y=weight))+geom_point()+
geom_smooth(method = "lm", se = FALSE)+
ylim(70,160)+
xlim(NA, 2070)+
theme_ipsum()+
annotate(geom = "richtext", label = "<i>Andrew Johnson</i> <br>1868. <br> <b>Weight</b>: 79kg",
x = 1882, y = 80)+ annotation_custom(johnson_grab,xmin=1895, xmax=1920,ymin=82, ymax=92)+
annotate(geom = "richtext", label = "<i>Bill Clinton</i> <br>1998.<br> <b>Weight</b>: 102kg",
x = 1998, y = 102)+
annotation_custom(clinton_grab,xmin=2008, xmax=2028,ymin=103, ymax=114)+
annotation_custom(trump_grab,xmin=2010, xmax=2030,ymin=134, ymax=144)+
annotate(geom = "richtext", label = "<i>Donald Trump</i> <br>2019, 2021<br> <b>Weight</b>: 131kg",
x = 2019, y = 131)+
annotation_custom(forecast_grab,xmin=2042, xmax=2062,ymin=146, ymax=156)+
annotate(geom = "richtext", label = "<i>Forecast</i> <br>2048<br> <b>Weight</b>: 138kg",
x = 2052, y = 142)+
xlab("Year of impeachment")+
ylab("Weight in kg at impeachment")+
theme(text = element_text(size=15))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment