Advertisement
csortu

R code for analysing historical GDP-per-capita data2

Nov 24th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.24 KB | None | 0 0
  1. library(readxl)
  2.  
  3. datfile <- "mpd_2013-01.xlsx"
  4. #excel_sheets(datfile)
  5.  
  6. gdppc <- read_excel(datfile,sheet = 2,skip=2)
  7. names(gdppc)[1] <- 'year'
  8.  
  9. # Drop data <1929 and >2009
  10. gdppc <- gdppc[148:227,]
  11.  
  12. library(reshape2)
  13. compara <- melt(gdppc[,1:7],
  14. #compara <- melt(gdppc,
  15.                 id="year",
  16.                 variable.name = "Country",
  17.                 value.name = "GDPPC")
  18.  
  19. library(ggplot2)
  20.  
  21. myplot <- ggplot(data=compara, aes(x=year,y=GDPPC,color=Country))
  22.  
  23. myplot +
  24.   geom_line() +
  25.   ggtitle("Economic history of major Caribbean countries") +
  26.   labs(x="Year",y="GDP / capita  \n[USD]")
  27.  
  28. gdppcrel <- gdppc
  29.  
  30. for (i in 2:8){
  31.   gdppcrel[,i] <- 100*gdppcrel[,i]/gdppcrel[30,i]
  32. }
  33.  
  34. compara2 <- melt(gdppcrel,
  35.                  id="year",
  36.                  variable.name = "Country",
  37.                  value.name = "GDPPCrel")
  38.  
  39. library(grid) # needed for arrow function
  40.  
  41. myplot2 <- ggplot(data=compara2, aes(x=year,y=GDPPCrel,color=Country))
  42.  
  43. yplot2 +
  44.   geom_line() +
  45.   ggtitle("Economic history of major Caribbean countries") +
  46.   labs(x="Year",y="GDP / capita  \n[% of value in 1958]") +
  47.   geom_segment(aes(x=1950,y=200,xend=1972,yend=170),
  48.                color='#00BFC4',
  49.                arrow=arrow(length = unit(10, "points"))) +
  50.   annotate(geom="text",x=1950, y=200,
  51.            label="People's National Party\nin Jamaica",
  52.            color="#00BFC4") +
  53.   geom_segment(aes(x=1940,y=130,xend=1958,yend=100),
  54.                color='#F8766D',
  55.                arrow=arrow(length = unit(10, "points"))) +
  56.   annotate(geom="text",x=1940, y=130,
  57.            label="Castro takes over\nCuba",
  58.            color="#F8766D") +
  59.   geom_segment(aes(x=1980,y=300,xend=1980,yend=250),
  60.                color='#619CFF',
  61.                arrow=arrow(length = unit(10, "points"))) +
  62.   annotate(geom="text",x=1980, y=310,
  63.            label="Latin American debt crisis",
  64.            color="#619CFF")
  65. myplot2 +
  66.   geom_line() +
  67.   ggtitle("Economic history of major Caribbean countries") +
  68.   labs(x="Year",y="GDP / capita  \n[% of value in 1958]") +
  69.   geom_segment(aes(x=1950,y=200,xend=1972,yend=170),
  70.                color='#00BFC4',
  71.                arrow=arrow(length = unit(10, "points"))) +
  72.   annotate(geom="text",x=1950, y=200,
  73.            label="People's National Party\nin Jamaica",
  74.            color="#00BFC4") +
  75.   geom_segment(aes(x=1940,y=130,xend=1958,yend=100),
  76.                color='#F8766D',
  77.                arrow=arrow(length = unit(10, "points"))) +
  78.   annotate(geom="text",x=1940, y=130,
  79.            label="Castro takes over\nCuba",
  80.            color="#F8766D") +
  81.   geom_segment(aes(x=1980,y=300,xend=1980,yend=250),
  82.                color='#619CFF',
  83.                arrow=arrow(length = unit(10, "points"))) +
  84.   annotate(geom="text",x=1980, y=310,
  85.            label="Latin American debt crisis",
  86.            color="#619CFF") +
  87.   geom_segment(aes(x=1940,y=40,xend=2010,yend=300),
  88.                color='black',
  89.                arrow=arrow(length = unit(10, "points"))) +
  90.   geom_segment(aes(x=1960,y=100,xend=2010,yend=150),
  91.                color='#F8766D',
  92.                arrow=arrow(length = unit(10, "points"))) +
  93.   geom_segment(aes(x=1972,y=160,xend=2010,yend=160),
  94.              color='#00BFC4',
  95.              arrow=arrow(length = unit(10, "points")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement