Skip to content

Instantly share code, notes, and snippets.

@diegovalle
Created February 25, 2010 03:34
Show Gist options
  • Save diegovalle/314200 to your computer and use it in GitHub Desktop.
Save diegovalle/314200 to your computer and use it in GitHub Desktop.
Pretty plots of Mexico's gdp
########################################################
##### Author: Diego Valle Jones
##### Website: www.diegovalle.net
##### Date Created: Mon Feb 22 19:11:54 2010
########################################################
#Pretty plots of Mexico's gdp
library(ggplot2)
#deseasonalized quarterly gdp 1993-2009 in 2003 pesos
#To recreate the series go to http://dgcnesyp.inegi.gob.mx/
#and upload a file with the following text
#BIE&156892&A050008007002400050&1993&9999&1&NoBD&APAV&T&1&T&2&F
pib93.09 <- c(5832539,5859438,5870842,5922171,6026731,
6168791,6168672,6243711,5924119,5656017,
5706646,5794417,5989600,6015083,6086314,
6249625,6349465,6460729,6578229,6713016,
6782885,6841268,6890065,6890911,6965273,
7052020,7133586,7231394,7451101,7512777,
7581543,7537599,7467707,7458046,7461699,
7413809,7369532,7437084,7502662,7511297,
7517176,7557444,7528877,7624103,7766507,
7837081,7871201,7973319,8035243,8019018,
8140080,8252757,8388687,8560104,8529735,
8568406,8637078,8824461,8823751,8893651,
9000985,8967046,8957448,8785683,8178970,
8201688,8407884,8578782)
#deseasonalized quarterly GDP 1980-2007 in 1993 pesos
#To recreate the series go to http://dgcnesyp.inegi.gob.mx/
#and upload a file with the following text
#BIE&65575&Z100250010000900070&1980&9999&1&NoBD&APAV&T&1&F&2&F
pib80.03 <- c(927204,933253,953400,981009,1002915,1028901,
1034280,1052810,1032525,1034098,1026661,
1004133,996709,976668,984776,996105,1023599,
1008533,1032955,1023852,1042579,1041676,
1048191,1045345,1025188,1021359,1001971,
999236,1005206,1032711,1033816,1046308,
1039240,1035906,1037185,1057848,1077668,
1078080,1098177,1088493,1112160,1136292,
1151792,1166431,1169646,1187544,1189752,
1211241,1211135,1230996,1242426,1243461,
1248740,1242982,1263034,1269334,1285259,
1307822,1320061,1334531,1271831,1196769,
1212500,1240129,1273002,1276052,1296610,
1328389,1348595,1367968,1389775,1418062,
1434626,1445191,1458327,1458540,1472421,
1490981,1518080,1538995,1580136,1604220,
1621297,1613099,1613526,1605433,1598065,
1591643,1597857,1615610,1624298,1623083,
1617482,1633372,1641048,1656652,1677337,
1694260,1714033,1736770,1738741,1727645,
1767374,1779976,1814432,1835541,1845089,
1856468,1864123,1888614,1913150,1927332
)
#Census population for 1980
pop80 <- 66846833
#Midyear population 1990-2009 from CONAPO
pop90.09 <- c(83971014, 85583336, 87184832, 88752014, 90265775,
91724528, 93130089, 94478046, 95790135, 97114831, 98438557,
99715527, 100909374, 101999555, 103001867, 103946866,
104874282, 105790725, 106682518, 107550697)
#Use loess to estimat the population from 1980 to 1989
pop <- data.frame(year = c(1980, 1990:2000),
pop = c(pop80, pop90.09[1:11]))
lm.lo <- loess(pop ~ year, data = pop)
new.years <- data.frame(year = c(1981:1989))
pr <- predict(lm.lo, new.years)
pop <- c(pop80, pr, pop90.09)
plot(pop, type="l")
#Use a linear regression to convert the series with gdp in
#1993 pesos to 2003 pesos
joit <- data.frame(old = log(pib80.03[53:72]),
new = log(pib93.09[1:20]) )
lm.out <- lm(new ~ old, data = joit)
#plot(lm.out)
new.gdp <- data.frame(old = log(pib80.03[1:52]))
pr <- predict(lm.out, new.gdp)
pr <- exp(pr)
com <- c(pr, pib93.09)
plot(com)
#Presidential terms
pomex <- data.frame(start = seq(1976, 2007, 6),
end = seq(1982, 2013, 6),
name = c("Lopez Portillo", "de la Madrid",
"Salinas",
"Zedillo", "Fox", "Calderon"))
#GDP
gdp <- data.frame(year = rep(1980:2009, each = 4),
gdp = com)
yrng <- range(gdp$gdp) + c(-30000, 30000)
ggplot(gdp, aes(seq(1980, 2009.75, .25), gdp)) +
geom_text(aes(x = pomex$start + .75, y = yrng[2], label = name),
data = pomex, size = 3, hjust = 0, vjust = 0) +
geom_line(size = 1.2) + ylab("") + xlab("") +
scale_x_continuous(breaks=pomex$start + .75,
labels =paste(pomex$start,c(".IV"), sep ="")) +
opts(title = "Quarterly GDP 1980-2009") +
theme_bw()
dev.print(png, file="gdp.png", width=640, height=480)
#GDP per Capita
gm <- function(vec){
exp(mean(log(vec)))
}
yearly <- ddply(gdp, .(year), function(df) {gm(df$gdp))})
yearly$capita <- yearly$V1/pop*1000000
yrng <- range(yearly$capita) + c(-1000, 1000)
ggplot(yearly, aes(year, capita)) +
geom_text(aes(x = pomex$start, y = yrng[2], label = name),
data = pomex, size = 3, hjust = 0, vjust = 0) +
geom_line(size = 1.2) +
scale_x_continuous(breaks=pomex$start) +
opts(title = "GDP per capita 1980-2009") +
xlab("") + ylab("") +
theme_bw()
dev.print(png, file="80-09.png", width=640, height=480)
#GDP per capita since 2000
yearly00 <- subset(yearly, year >= 2000)
ggplot(yearly00, aes(year, capita / capita[1])) +
geom_line(size = 1.2) +
scale_y_continuous(formatter="percent") +
geom_hline(yintercept=1) +
opts(title = "GDP per capita (2000 = 100%)") +
ylab("") +
theme_bw()
dev.print(png, file="2000.png", width=640, height=480)
#GDP per capita growth since NAFTA
yearly94 <- subset(yearly, year >= 1994)
qplot(yearly94$year, yearly94$capita / yearly94$capita[1],
geom="line") +
scale_y_continuous(formatter="percent")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment