############## #R Skript für den Blogbeitrag: #Der wachsende Graben der G20 #Kontakt: basil.schlaepfer at uzh.ch ############## rm(list=ls(all=TRUE)) #Weltbankdaten laden: file==wdi.csv #(damit die Grafiken auch bei einer Aktualisierung der Weltbnk #daten stimmen, werden die verwendeten Daten abgeben und nicht der API-Zugang dargestellt. Weltbankd#aten könen sich ändern, wenn zum Beispiel die Angaben zu konstanten Dollar in einem neuen Jahr #fixiert werden.) options(stringsAsFactors = FALSE) daten <- read.csv(file.choose(), sep=',') #Drop Argentina daten <- daten[daten$country!='Argentina',] ############ #Plotly API# ############ #library("devtools") #install_github("plotly", "ropensci") library("plotly") py <- plotly("RgraphingAPI", "ektgzomjbx") ##################################################### #1. Grafik unterschied Industrie und Schwellenländer# ##################################################### #Datenaufbereitung mat <- matrix(NA,24,3) r <- 0 rich <- c(rep(NA,24)) for (i in unique(daten$year)){ r <- r+1 temp <- subset(daten, year==i) temp$dummy <- temp$differenzpp temp$dummy[temp$country=="Korea, Rep." ] <- -100 temp <- temp[which(temp$country!="Switzerland" ),] temp2 <- subset(temp, dummy>0) durchreich <- mean(temp2$differenzpp) rich[r] <- mean(temp2$NY.GDP.PCAP.PP.KD) temp3 <- subset(temp, dummy<0) durcharm <- mean(temp3$differenzpp) mat[r,1] <- i mat[r,2] <- durchreich mat[r,3] <- durcharm } plotmat <- data.frame(mat[c(1:23),]) matrich <- mat[c(1:23),c(1:2)] matpoor <- mat[c(1:23),c(1,3)] mat2 <- rbind(matrich, matpoor) namen <- c(rep("Industrienationen (G20)", 23),rep("Schwellenländer (G20, ohne Argentinien)",23)) mat3 <- data.frame(mat2) mat3$gruppe <- namen #Plot erstellen p <- plotly(username="username", key="key") trace0 <- list( x = c(mat3$X1[mat3$gruppe=="Industrienationen (G20)"]), y = c(mat3$X2[mat3$gruppe=="Industrienationen (G20)"]) ) trace1 <- list( x = c(mat3$X1[mat3$gruppe=="Schwellenländer (G20, ohne Argentinien)"]), y = c(mat3$X2[mat3$gruppe=="Schwellenländer (G20, ohne Argentinien)"]), yaxis = "y2", xaxis = "x2" ) layout <- list( yaxis = list(domain = c(0.60, 1)), yaxis2 = list(domain = c(0, 0.40)), xaxis2=list(anchor="y2"), showlegend = TRUE, legend = list( yaxis = list(autorange = "reversed"), x = 1, y = 1 ) ) response <- p$plotly(trace0,trace1, kwargs=list(layout=layout, filename="... ", fileopt="overwrite")) url <- response$url filename <- response$filename ################ #2. Grafik: GIF# ################ daten$country <- reorder(daten$country, daten$diffpppmean) daten5 <- subset(daten,daten$country!='Switzerland') #Schnitt G20 lauf<-unique(unlist(daten5$year)) for (i in lauf){ daten5$diffpp[daten5$year==i] <- daten5$NY.GDP.PCAP.PP.KD[daten5$year==i]-mean(daten5$NY.GDP.PCAP.PP.KD[daten5$year==i]) } #Eigentliches GIF lauf <- sort(lauf, decreasing = FALSE) setwd() for(i in 1:length(lauf)){ temp <- subset(daten5,year==lauf[i]) temp$country2 <- as.character(temp$country) temp$country2[temp$country2=='Russian Federation'] <- 'Russia' temp$country2[temp$country2=='Korea, Rep.'] <- 'S.Korea' temp$country2[temp$country2=='United Kingdom'] <- 'UK' temp$country2[temp$country2=='United States'] <- 'USA' temp$country2[temp$country2=='South Africa'] <- 'S.Africa' temp$country2[temp$country2=='Saudi Arabia'] <- 'S.Arabia' temp$country2 <- reorder(temp$country2, temp$diffpppmean) #Die Titel der einzelnen Frames sollen das Jahr enthalten jahr <- lauf[i] title <- paste("Differenz zum durchschnittlichen BIP per capita ($)",jahr, sep=" ") #Eigentlicher plot p <- ggplot(temp, aes(x=country2, y=diffpp)) + geom_bar(color='#004C99', fill='#0066CC') + theme_bw() + scale_y_continuous("",limits=c(-28000, 28000)) + ggtitle(title) + theme( plot.title = element_text(face="bold", size=100), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x=element_text(size=80,angle=30), axis.text.y=element_text(size=80), legend.position="none", panel.background = element_blank(), text=element_text(size=16, family="Cambria") ) p filename <- sprintf("Pfad festlegen/dat_%s.jpg",i) ggsave(file=filename, width=45, height=30, dpi=400) } ############################################## #3. Grafik: Grafik: Wie viel ist ein 1% wert?# ############################################## temp <- subset(daten, year==2012 & country!='Switzerland') temp$wert <- temp$NY.GDP.PCAP.PP.KD*0.01 temp$country2 <- as.character(temp$country) temp$country2[temp$country2=='Russian Federation'] <- 'Russia' temp$country2[temp$country2=='Korea, Rep.'] <- 'S.Korea' temp$country2[temp$country2=='United Kingdom'] <- 'UK' temp$country2[temp$country2=='United States'] <- 'USA' temp$country2[temp$country2=='South Africa'] <- 'S.Africa' temp$country2[temp$country2=='Saudi Arabia'] <- 'S.Arabia' temp$country2 <- reorder(temp$country2, temp$diffpppmean) p1 <- ggplot(data=temp, aes(x=country2, y=wert)) + geom_bar(stat='identity') + theme_bw() py$ggplotly(p1) ###################################################### #4. Grafik: Benötigtes vs. Realsiertes Wachstum China# ###################################################### #Differenz der reichen daten$meanrich <- NA for (i in unique(daten$year)){ daten$bin <- daten$differenzpp daten$bin[daten$country=="Korea, Rep." ] <- -100 daten$bin[daten$country=="Switzerland" ] <- 0 daten$meanrich[daten$year==i] <- mean(daten$NY.GDP.PCAP.PP.KD[daten$bin>0 & daten$year==i]) } lagmeanrich <- c(NA,daten$meanrich[1:(length(daten$meanrich)-1)]) daten$lagmeanrich <- lagmeanrich daten$diffmeanrich <- daten$meanrich-daten$lagmeanrich bippplag <- c(NA,daten$NY.GDP.PCAP.PP.KD[1:(length(daten$NY.GDP.PCAP.PP.KD)-1)]) daten$bippplag <- bippplag datenlag <- subset(daten, year!=1990) datenlag$needrich <- datenlag$diffmeanrich/datenlag$bippplag*100 datenlag$needrich <- round(datenlag$needrich, digits=1) datenlag$usbipppgr <- round(datenlag$usbipppgr, digits=1) daten$usbipppgr <- NA daten$usbipppgr[daten$country=='China'] <- (daten$NY.GDP.PCAP.PP.KD[daten$country=='China'] - daten$bipplag[daten$country=='China']) temp6 <-subset(datenlag, country=='China') temp6$color1 <- 'lightblue' temp6$color2 <- 'red' plot <- ggplot(data=temp6, aes(year,diffmeanrich)) + geom_bar(stat='identity', color=temp6$color1, fill='lightblue') + geom_line(data=temp6,aes(year,usbipppgr), color=temp6$color2)+ coord_cartesian(xlim = c(1990.5, 2012.5)) + theme_bw() + scale_y_continuous(name="") + ggtitle("China: Benötigtes vs. realisiertes Wachstum") + theme( axis.title.y = element_text(size=15), axis.ticks.x=element_blank(), axis.text.x = element_text(size=15), axis.text.y = element_text(size=15), axis.title.x=element_blank(), plot.title = element_text(face="bold", vjust=2, hjust=-0.09) ) plot #Auf Plotly laden: py$ggplotly(plot) ############## #5. Grafik: Erwartetes absolutes BIP-Wachstum ############## library(reshape) #IMF Daten einlesen, file==weoreptc.csv imf <- read.csv(file.choose(), sep=';') imf2 <- melt(imf, id.vars=c("Country", "dummy"), measure.vars=c("X2012", "X2013", "X2014", "X2015", "X2016", "X2017", "X2018", "X2019")) imf2$year <- as.numeric(as.character(gsub("X","", imf2$variable))) imf2$dummy[imf2$Country=="China"] <- 0 imf3 <- subset(imf2, dummy==0) imf3 <- imf3[with(imf3,order(Country, year)),] lagvec <- c(NA,imf3$value) lagvec <- lagvec[1:(length(lagvec)-1)] imf3$lagvalue <- lagvec imf3$diff <- imf3$value-imf3$lagvalue imf4 <- subset(imf3, year!=2012) imf4$diff <- round(imf4$diff, 0) plotdata <- subset(imf4, Country!="Switzerland") #Plot erstellen py <- plotly("RgraphingAPI", "ektgzomjbx") graph <- ggplot(data=plotdata) + geom_line(data=plotdata[plotdata$Country=='China',],aes(year,diff), name='China')+ geom_line(data=plotdata[plotdata$Country=='France',],aes(year,diff), name='France')+ geom_line(data=plotdata[plotdata$Country=='Italy',],aes(year,diff), name='Italy')+ geom_line(data=plotdata[plotdata$Country=='Germany',],aes(year,diff), name='Germany')+ geom_line(data=plotdata[plotdata$Country=='United Kingdom',],aes(year,diff), name='Kingdom')+ geom_line(data=plotdata[plotdata$Country=='United States',],aes(year,diff), name='United States')+ geom_line(data=plotdata[plotdata$Country=='Japan',],aes(year,diff), name='Japan')+ geom_line(data=plotdata[plotdata$Country=='Australia',],aes(year,diff), name='Australia')+ geom_line(data=plotdata[plotdata$Country=='Saudi Arabia',],aes(year,diff), name='Saudi Arabia')+ geom_line(data=plotdata[plotdata$Country=='Canada',],aes(year,diff), name='Canada')+ theme_bw() graph py$ggplotly(graph)