require(ggplot2)
di_rank<-c(1:25)
country<-c("Norway", "Australia", "Switzerland", "Netherlands", "United States",
"Germany", "New Zealand", "Canada", "Singapore", "Denmark",
"Ireland", "Sweden", "Iceland", "England", "South Korea",
"Japan", "Israel", "France", "Austria", "Belgium",
"Luxembourg", "Finland", "Slovenia", "Italy", "Spain")
guns_100<-c(31.3, 15.0, 45.7, 3.9, 88.8,
30.3, 22.6, 30.8, 0.5, 12.0,
8.6, 31.6, 30.3, 6.2, 1.1,
0.6, 7.3, 31.2, 30.4, 17.2,
15.3, 45.3, 13.5, 11.9, 10.4)
gun_homicides_100<-c(0.05, 0.14, 0.77, 0.33, 3.2,
0.19, 0.16, 0.51, 0.02, 0.27,
0.48, 0.41, 0.0, 0.07, 0.03,
0.01, 0.09, 0.06, 0.22, 0.68,
0.62, 0.45, 0.10, 0.71, 0.20)
guns<-data.frame(rank=di_rank, country=country, guns_100=guns_100, gun_murder_100=gun_homicides_100)
p<-ggplot(guns, aes(guns_100, gun_murder_100, label=country))
p + geom_point() + theme_bw() + labs(x= "Guns per 100 people", y="Homicides by gun per 100 people")
country[5]<-"USA"
country[7]<-"New-Zealand"
country[14]<-"United-Kingdom"
country[15]<-"South-Korea"
guns<-data.frame(rank=di_rank, country=country, guns_100=guns_100, gun_murder_100=gun_homicides_100,row.names=country)
require(RCurl)
require(png)
flags <- list()
for(i in country){
#I expext there's a better way to look up the flag unicodes than this....
html <- getURLContent(paste0("http://emojipedia.org/flag-for-",i))
pt1 <- substr(html,401,401)
pt2 <- substr(html,402,402)
extract_short_name<-function(multibyte_char1,multibyte_char2){
shorten<-function(mb){
out<-character_string<-eval(parse(text=gsub("\\", "", deparse(mb), fixed=TRUE)))
short<-substr(out,5,10)
short}
paste0(shorten(multibyte_char1),"-",shorten(multibyte_char2))}
name<-extract_short_name(pt1,pt2)
#but it works
#so use them to harvest some flags
URL<-paste0("https://raw.githubusercontent.com/Ranks/emojione/master/assets/png/",toupper(name),".png")
flags[[i]] <- readPNG(getURLContent(URL))
}
p<-ggplot(guns, aes(guns_100, gun_murder_100, label=country))
p<-p+geom_point() + theme_bw() + labs(x= "Guns per 100 people", y="Homicides by gun per 100 people")
for(i in country){
xy <- guns[i, 3:4]
p<-p+annotation_raster(flags[[i]],xmin=xy[[1]], xmax=xy[[1]]+4, ymin=xy[[2]], ymax=xy[[2]]+0.2,interpolate=T)
}
p
(Emoji set designed and offered free by Emoji One)