Created
January 14, 2013 17:02
-
-
Save joelgombin/4531536 to your computer and use it in GitHub Desktop.
R code for the Dataviz MOOC exercise, week 1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
top10 <- structure(list(Country = structure(c(10L, 2L, 9L, 3L, 6L, 7L, | |
8L, 4L, 5L, 1L), .Label = c("Brazil", "China", "France", "Germany", | |
"India", "Japan", "Russia", "Saudi Arabia", "UK", "USA"), class = "factor"), | |
Budget = c(739.3, 89.8, 62.7, 58.8, 58.4, 52.7, 46.2, 44.2, | |
37.3, 36.6)), .Names = c("Country", "Budget"), class = "data.frame", row.names = c(NA, | |
-10L)) | |
top10$Country <- factor(top10$Country, levels=c(as.character(rev(top10$Country)))) | |
png(filename="fig1.png",600,600,res=72) | |
ggplot(top10, aes(x=Country, y=Budget)) + geom_bar(stat="identity", fill="darkgrey") + coord_flip() + labs(title = "The overwhelming US military domination:\nTop 10 Defence Budgets in 2011") + theme_tufte() + theme(plot.title = element_text(lineheight=1, face="bold", hjust=0),axis.text = element_text(size=12), axis.title = element_text(size=12)) + xlab("") + ylab("Budget (in US$bn)") + geom_text(aes(label = Budget),hjust=1.1, family="serif") | |
dev.off() | |
top10relative <- structure(list(Country = structure(c(8L, 7L, 4L, 10L, 9L, 5L, | |
1L, 3L, 6L, 2L), .Label = c("Algeria", "Armenia", "Iraq", "Israel", | |
"Jordan", "Myanmar", "Oman", "Saudi Arabia", "USA", "Yemen"), class = "factor"), | |
Budget = c(8.26, 6.42, 5.99, 5.5, 4.91, 4.82, 4.47, 4.46, | |
4.42, 3.77)), .Names = c("Country", "Budget"), class = "data.frame", row.names = c(NA, | |
-10L)) | |
top10relative$Country <- factor(top10relative$Country,levels=rev(as.character(top10relative$Country))) | |
png(filename="fig2.png",600,600,res=72) | |
ggplot(top10relative, aes(x=Country, y=Budget)) + geom_bar(stat="identity", fill="darkgrey") + coord_flip() + labs(title = "The strongest defence efforts:\nTop 10 Defence Budgets in 2011, as a % of GDP") + theme_tufte() + theme(plot.title = element_text(lineheight=1, face="bold", hjust=0),axis.text = element_text(size=12), axis.title = element_text(size=12)) + xlab("") + ylab("Budget (as a % of GDP)") + geom_text(aes(label = Budget),hjust=1.1, family="serif") | |
dev.off() | |
regions <- structure(list(Country = structure(c(16L, 15L, 3L, 4L, 7L, 8L, | |
2L, 6L, 5L, 9L, 12L, 13L, 11L, 1L, 10L, 14L), .Label = c("Brazil", | |
"China", "France", "Germany", "India", "Japan", "NATO Europe", | |
"Non NATO Europe", "Other Asia and Australasia", "Other Latin America and the Carribean", | |
"Other Middle East and North Africa", "Russia", "Saudi Arabia", | |
"Sub-Saharan Africa", "UK", "USA"), class = "factor"), Continent = structure(c(5L, | |
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 6L, 4L, 4L, 3L, 3L, 7L), .Label = c("Asia", | |
"Europe", "Latin America and the Carribean", "Middle East and North Africa", | |
"North America", "Russia", "Sub-Saharan Africa"), class = "factor"), | |
Share = c(45.7, 3.9, 3.6, 2.7, 7.8, 1.6, 5.5, 3.6, 2.3, 7, | |
3.3, 2.9, 5, 2.3, 1.8, 1)), .Names = c("Country", "Continent", | |
"Share"), class = "data.frame", row.names = c(NA, -16L)) | |
regions$Country <- factor(regions$Country, levels=rev(as.character(regions$Country))) | |
regions$Continent <- factor(regions$Continent, levels = unique(as.character(regions$Continent))) | |
library(plyr) | |
regions$coord <- ddply(regions, .(Continent), summarize, (cumsum(Share) + cumsum(Share)-Share)/2)[,2] | |
regions$Continent <- factor(regions$Continent, levels = rev(c("Sub-Saharan Africa","Russia", "Latin America and the Carribean", "Middle East and North Africa", "Asia","Europe", "North America"))) | |
levels(regions$Continent) <- rev(c("Sub-Saharan Africa","Russia", "Latin America and\nthe Carribean", "Middle East and\n North Africa", "Asia","Europe", "North America")) | |
levels(regions$Country)[c(1,2,4,7,11)] <- c("Sub-Saharan\nAfrica", "Other Latin America\nand the Caribbean", "Other Middle East\nand North Africa", "Other Asia and\nAustralasia", "Non Nato\nEurope") | |
library(scales) | |
png(filename="fig3.png",1000,600,res=72) | |
ggplot(regions, aes(x=Continent, y=Share, fill=Country)) + geom_bar(stat="identity") + labs(title = "The West dominates the World's Defence Spendings: \nPlanned Global Defence Expenditures by Region and Country as % of global spendings, 2011") + theme_tufte() + theme(plot.title = element_text(lineheight=1, face="bold", hjust=0,size=18),axis.text = element_text(size=12), legend.position="none") + xlab("") + ylab("") + geom_text(aes(y=coord,label = Country),vjust=0.5) + scale_y_continuous(labels=function(x) return(paste(x, " %", sep=""))) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment