knitr::opts_chunk$set( echo = FALSE, message = FALSE, warning = FALSE, # dev = "svg", fig.width = 12, fig.height = 12 # fig.retina = 3 ) print3plots<-function(plotlist, datadl, title00, legends = c(T,T,T,T), titles = c(F,F,F,F)){ b0<-list() for (i in 1:length(plotlist)){ b<-plotlist[i][[1]] if (names(plotlist)[i] %in% "blank") { b<-b+ geom_line(aes(group = cat), size = 2) + theme_classic(base_size = 25) if(legends[i] %in% F) { b<-b + theme(legend.position = "none") } else { b<-b + theme(legend.title = element_blank()) } if(titles[i] %in% F) { b<-b + ggtitle("") } } b0<-c(b0, list(b)) } b<-plotlist layout <- (b0[1][[1]]) / (b0[2][[1]]) / (b0[3][[1]]) title0<-strsplit(split = "_", x = names(b)[1])[[1]][-c(1,2)] title0<-title0[-length(title0)] title0<-gsub(pattern = "byr", replacement = "base yr = ", x = title0) title0<-paste(title0, collapse = " ") title0<-gsub(pattern = " P ", replacement = " Price Method ", x = title0) title0<-gsub(pattern = " Q ", replacement = " Quantity Method ", x = title0) layout + plot_annotation( title = title00, subtitle = "", caption = paste0("Data downloaded from FOSS ", datadl, ". "), theme = theme(plot.title = element_text(size = 20, hjust = 0.5, face = "bold")) ) } plotnlines<-function(dat, title00, place){ xnames<-as.numeric(paste0(dat$Year)) xnames[!(xnames %in% seq(from = min((as.numeric(xnames))), to = max(as.numeric(xnames)), by = 10))]<-"" dat$val<-as.numeric(as.character(dat$val)) dat$val[(is.infinite(dat$val))]<-NA divideby<-paste0("(", strsplit(x = xunits(mean(dat$val, na.rm = T)), split = " ")[[1]][2], "s)") if (divideby %in% "(trillions)") { divideby0<-1e12 } else if (divideby %in% "(billions)") { divideby0<-1e9 } else if (divideby %in% "(millions)") { divideby0<-1e6 } else if (divideby %in% "(thousands)") { divideby0<-1e3 } else if (divideby %in% "(NAs)") { divideby0<-1 divideby<-"" } dat$val<-dat$val/divideby0 # ynames<-as.numeric(paste0(val)) g<-ggplot(data = dat, aes(x = factor(Year), y = val, color = cat)) + geom_line(aes(group = cat), size = 3) + geom_point() + theme_classic(base_size = 20) + theme( legend.title = element_blank(), # legend.position = c(0.9, 0.2), panel.grid.major.y = element_line( color=NOAALightBlue, size = .1 ), panel.grid.minor.y = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), axis.line = element_line( color=NOAALightBlue, size = .1 ), axis.ticks = element_blank(), # remove ticks panel.background = element_blank() ) + ylab(paste0(gsub(pattern = "_", replacement = "", x = strsplit(x = title00, split = "-")[[1]][1]), " ", divideby)) + xlab("Year") + scale_x_discrete(labels= xnames) + # scale_y_discrete(labels= ynames) + guides(fill=FALSE) + ggtitle(paste0(place)) return(g) } xunits<-function(temp00, combine=T) { temp00<-sum(as.numeric(temp00)) sigfig<-format(temp00, digits = 3, scientific = TRUE) sigfig0<-as.numeric(substr(x = sigfig, start = (nchar(sigfig)-1), stop = nchar(sigfig))) if (sigfig0<=5) { # if (sigfig0<4) { unit<-"" x<-format(x = temp00, big.mark = ",", digits = 0, scientific = F) # } else if (sigfig0>=4 & sigfig0<6) { # unit<-" thousand" # x<-round(temp00/1e3, digits = 1) # } else if (sigfig0==5) { # unit<-" thousand" # x<-round(temp00/1e3, digits = 0) } else if (sigfig0>=6 & sigfig0<9) { unit<-" million" x<-round(temp00/1e6, digits = 1) } else if (sigfig0>=9 & sigfig0<12) { unit<-" billion" x<-round(temp00/1e9, digits = 1) } else if (sigfig0>=12) { unit<-" trillion" x<-round(temp00/1e12, digits = 1) } out<-ifelse(combine==T, paste0(x, unit), list(x, unit)) return(out) } plotcrossanalysis<-function(dat0, place, datadl){ #Q dat<-dat0[,c(1, grep(pattern = "Q ", x = names(dat0)), which(names(dat0) %in% "QE"))] temp0<-gather(dat, cat, val, names(dat)[2]:names(dat)[length(names(dat))], factor_key = T) temp0$cat<-gsub(pattern = "Q ", replacement = "", x = temp0$cat) temp0$cat<-gsub(pattern = "[[:punct:]]", replacement = "", x = temp0$cat) Q<-plotnlines(dat = temp0, title00 = "Q", place = "") # #QE # dat<-dat0[,c(1, grep(pattern = "QE", x = names(dat0)))] # # temp0<-gather(dat, cat, val, # names(dat)[2]:names(dat)[length(names(dat))], # factor_key = T) # # QE<-plotnlines(dat = temp0, title00 = "QE", place = place) #QI dat<-dat0[,c(1, grep(pattern = "QI ", x = names(dat0)), grep(pattern = "QEI", x = names(dat0)))] temp0<-gather(dat, cat, val, names(dat)[2]:names(dat)[length(names(dat))], factor_key = T) temp0$cat<-gsub(pattern = "QI ", replacement = "", x = temp0$cat) temp0$cat<-gsub(pattern = "[[:punct:]]", replacement = "", x = temp0$cat) QI<-plotnlines(dat = temp0, title00 = "QI", place = "") #PI dat<-dat0[,c(1, grep(pattern = "PI ", x = names(dat0)))] temp0<-gather(dat, cat, val, names(dat)[2]:names(dat)[length(names(dat))], factor_key = T) temp0$cat<-gsub(pattern = "PI ", replacement = "", x = temp0$cat) temp0$cat<-gsub(pattern = "[[:punct:]]", replacement = "", x = temp0$cat) PI<-plotnlines(dat = temp0, title00 = "PI", place = "") # #blank # blank<-ggplot(dat = data.frame(x=1,y=1), aes(x, y)) + # theme(panel.background = element_blank(), # axis.title = element_blank(), # axis.text = element_blank(), # axis.ticks = element_blank()) + # ggtitle("") plotlist<-list("Q" = Q, "QI" = QI, # "blank" = blank, # "QE" = QE, "PI" = PI) print3plots(plotlist = plotlist, datadl, title00 = "") } counter<-1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
r reg.order[counter]
r gsub(pattern = "To", replace = "-", yr)
place<-reg.order[counter] if (place %in% "Pacific") { temp000<-strsplit(x = names(summarydata), split = " ") temp000<-unlist(lapply(temp000, `[[`, 1)) temp000<-strsplit(x = temp000, split = "_") temp000<-unlist(lapply(temp000, `[[`, 1)) dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & temp000 %in% place][[1]] } else { dat0<-summarydata[grepl(pattern = yr, x = names(summarydata)) & grepl(pattern = place, x = names(summarydata))][[1]] } plotcrossanalysis(dat0, place, datadl) counter<-counter+1
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.