About Me

My photo
Just wasting time sharing knowledge about, Big Data and Analytics
Showing posts with label ggplot. Show all posts
Showing posts with label ggplot. Show all posts

Jul 7, 2013

ggmap : Interesting toolbox for spatial analysis

ggmap is a new tool which enables such visualization by combining the spatial information of static maps from Google Maps, OpenStreetMap, Stamen Maps or CloudMade Maps with the layered grammar of graphics implementation of ggplot2

The library is developped by David Kahle and Hadley Wickham and in the latest R/Journal (Volume 5/1, June 2013), there is a whitepaper, very interesting that should to be read.

Let's use it to see where we can always buy tobacco at night in Paris.

Data are from data.ratp.fr, and give all authorized dealers by ratp. There's long and lat in this file, but this geocoding use lambert. So to show how ggmap work, we can extract adress, run geocoding using api/google and then ouput thematic map.

> require(ggmap)
> setwd("C:\\Users\\guibertt\\Desktop\\Geocodage - A faire")
> fic<-"commerces.csv"
> commerces<-read.csv(fic,header=T,sep=";", dec=",")
> head(commerces[,c(1,3:6)])
  DEA_CODE    ADRESSE_LIVRAISON DEA_CODE_POSTAL_LIVRAISON DEA_COMMUNE_LIVRAISON INSEE_LIVRAISON
1   510001       11   R. Mozart                     92230         Gennevilliers           92036
2   510002     81   Bd Voltaire                     92600    Asnières-sur-Seine           92004
3   510003 60   Av. Jean Moulin                     92390 Villeneuve-la-Garenne           92078
4   510004     2   Av. Michelet                     93400            Saint-Ouen           93070
5   510006      31   R. d'Anjou                     92600    Asnières-sur-Seine           92004
6   510007 45   R. Jules Larose                     92230         Gennevilliers           92036

We use the api google (limited at 2500 requests by day/ip adress) to geocode our places
>ad<-as.vector(tabac$adresse2)
>system.time(gc <- geocode(ad,output='latlona',messaging=FALSE))
> head(gc)
       lon      lat                                        address
1 2.267043 48.85066         108 avenue mozart, 75116 paris, france
2 2.297897 48.84552 56 rue de la croix nivert, 75015 paris, france
3 2.276529 48.84276             20 rue cauchy, 75015 paris, france
4 2.293242 48.83890  157 rue de la convention, 75015 paris, france
5 2.285362 48.83406       37 boulevard victor, 75015 paris, franc
>tabacp<-cbind(tabac,gc)
But, don't forget that ggmap is just a ggplot and we can get this, for example using OpenStreetMap
So we can now plot our places
png("paris4.png", width=800,height=600)
map <- get_map(location = 'paris',zoom=13,maptype="roadmap",color="color",source="google")
mymap = ggmap(map, darken = c(.3,'white'))
mymap+
  stat_bin2d(
aes(x = lon, y = lat, colour = typecommerce, fill = typecommerce),
size = .5, bins = 30, alpha = 1/2,
data = tabacp)
dev.off()



And at the end, with wrap, to split
png("paris5.png", width=800,height=600)
map <- get_map(location = 'paris',zoom=13,maptype="roadmap",color="bw",source="google")
mymap = ggmap(map, darken = c(.8,'white'))
mymap +  stat_bin2d(
aes(x = lon, y = lat, colour = TCO_LIBELLE, fill = TCO_LIBELLE),
size = .5, bins = 30, alpha = 1/2,
data = tabacp)+facet_wrap(~ TCO_LIBELLE)
dev.off()

May 12, 2013

A new package : Quandl

Quandl is a new database management tool which seeks to become the place to find datasets. That is, each unique indicator is considered an independent data set. This helps them to seem to have a ginormous quantity of data sets. Source : Blog Econometric Simulation.
To load or find the datasets, we have to authentify using the API like with Twitter First, we need to set an account to receive the Quandl.auth





setwd("D:/PERSO/R_Working/Tutoriels/Probability")
library(Quandl)
Quandl.auth("XXXXXXXXXX")  ### Replace with yours
For example, we load the database about pollution and gdp and try to find the link.

plot(date, pollution, col = "red", type = "o", lwd = 2, ylim = c(70, 150), ylab = "", 
    main = "Evolution du PIB vs Pollution entre 94 et 2006 au Japon")
lines(date, pib, lty = 2, col = "purple", type = "o")
legend("topright", legend = c("PIB", "Pollution"), col = c("purple", "red"), 
    pch = 15, bty = "n", pt.cex = 2, cex = 0.8, text.col = "black", horiz = TRUE, 
    inset = c(0.1, 0.1))


plot of chunk unnamed-chunk-3
We fit a model to know if the growth of GDP is responsible of pollution's growth in japan between 1994 and 2006.

plot of chunk unnamed-chunk-4

Ecologists are not totaly wrong ! ! !

The entire code to run this post
Share it ! ! !