R

From   

History

Ross had done his undergraduate degree at University of Auckland, then his masters and PhD at Berkeley, returning to a lecturing post in the Department of Statistics. In the early 1990s, internet was in its infancy and computers at the University of Auckland were boxy Macintoshes with floppy disks. Undergraduate students were using what Ross calls "old and clunky programmes" for their data analysis. He thought there had to be a better way. So when Canadian colleague Robert Gentleman stopped Ross in the corridor one day and suggested they write some software together, he agreed. Neither were programming experts, but in 1991 they started tinkering.

They eventually began creating "a basic structure that people could start plugging things into." What emerged was a user-friendly tool for students to do data analysis and produce graphical models of that information. They called it R, after the initials of their first names. It was released in 1993 and written in C, Fortran and R itself. R quickly became the mainstay of statistics classes, with student grumbles the opportunity to make improvements.

"Originally, our big ambition was to use it for teaching first-year classes -- small, local things."

Ross and Robert never commercialised R "because we couldn't deliver any final product, but we could provide the means for other people to develop a final product". When overseas colleagues became interested, they made R available to all. R is open-source, which means it's free and anyone can develop add-ons, as long as they share the underpinning source code. As of January 2016, there were more than 7,800 plug-ins.

Ross has described R as proof of the success of the "rusting-hulk model" of software development. If you went to a junkyard; hauled out an old junker; put it by the road; stood there looking helpless, people --being do-it-yourself types -- would step in and help you out. After a couple of hours, you'd have a pretty good car. "So we cobbled thing thing together and hung it out by the side of the internet. After a few years, he had a pretty good piece of software. It's the contribution of lots of people." In 1996, the pair wrote a paper, introducing R to the world. The acknowledgements at the back of the paper thank "all our colleagues and stutdents that were and still are our guinea pigs".

"With hindsight, you can see that here was a gap needing to be filled. In a sense, we came along at the perfect time. The internet was getting started. Free software was in the air. People were beginning to think about contributing to free projects. We had no plans for world domination. We shared it with students and it grew organically from there."

Usage

Installation

Install R Package

install.packages('packagename')

Pass a character vector to install several packages at once :
install.packages(c("package1","package2","package3"))

Show Map

require(osmdata)
require(ggmap)

target = "Pontarlier"
background_raster <- get_map(getbb(target), source = "stamen") 
ggmap(background_raster)
  • getbb(string) : From osmdata. Get bounding-box from place name string by using OSM's free Nominatim API

Download and Process OSM Data

require(osmdata)
require(sf)

item <- getbb("Place Name", format_out = "polygon") %>%
opq(osm_types = "way", timeout = 25) %>%
add_osm_feature(key = "leisure", value = "pitch") %>%
add_osm_feature(key = "sport", value = "tennis") %>%
osmdata_sf()
  •  %>%  : Pipe function. Left side will become right side's input argument.
  • opq : Build an Overpass Query
  • add_osm_feature : Add a feature to Overpass Query
  • osmdata_sf : Package osmdata object as SF format (ISO 19125-1:2004)

Data result :

> item
Object of class 'osmdata' with:
                 $bbox : 46.8756734,6.3114632,46.9576321,6.4478336
        $overpass_call : The call submitted to the overpass API
                 $meta : metadata including timestamp and version numbers
           $osm_points : 'sf' Simple Features Collection with 6319 points
            $osm_lines : NULL
         $osm_polygons : 'sf' Simple Features Collection with 640 polygons
       $osm_multilines : NULL
    $osm_multipolygons : NULL

Select the polygons :

polygons <- item$osm_polygons
summary(polygons$geometry)

Calculate area and add "area" column to "polygons"

polygons <- mutate(polygons, area = st_area(polygons$geometry))
summary(polygons$area)
  • st_area : From sf. Returns the area of a geometry, in the coordinate reference system used.

Filter columns :

polygons <- polygons %>%
select(osm_id,area)

Plot

plot(polygons$area)

Boxplot + Histogram + Frequency Polygon

require(dplyr)
require(osmdata)
require(sf)
require(ggmap)
require(units)
require(ggplot2)
require(patchwork)
require(svglite)

theme_osm <- function() {
  theme(
    #7ebc6f OSM green
    #7092ff OSM blue
    panel.background = element_blank(), #transparent panel bg
    plot.background = element_rect(fill='transparent', color=NA), #transparent plot bg
    panel.grid.major = element_blank(),
    legend.background = element_rect(fill='transparent'), #transparent legend bg
    legend.box.background = element_rect(fill='transparent'), #transparent legend panel
    axis.line = element_line(colour = "black")
  )
}

output_boxplot <- ggplot(polygons, aes(y=area)) +
  geom_boxplot(color="#7ebc6f") +
  coord_cartesian(ylim = c(0, 1500)) +
  labs(x=NULL, y="Surface") +
  theme_osm() +
  theme(aspect.ratio=5,
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

output_histo <- ggplot(polygons, aes(area)) +
  geom_histogram(binwidth = 100, color="#7ebc6f", fill="white") +
  coord_cartesian(xlim = c(0, 2000)) +
  labs(y=NULL, x="Surface", title=paste("Parkings à", target), caption="Source : OpenStreetMap©") +
  theme_osm()

output_plot <- output_histo + inset_element(output_boxplot, left = 0.6, bottom = 0.2, right = 0.95, top = 0.95)

ggsave(file="output_plot.svg", plot = output_plot, bg = "transparent")
output_boxplot
output_histo
output_plot
library(dplyr)
library(osmdata)
library(sf)
library(units)
library(ggplot2)

target="Doubs"
ggplot(polygons, aes(y=area)) +
  geom_boxplot() +
  labs(x=NULL, y="Surface", title=paste(target, "(Source : OpenStreetMap)")) +
  theme_light()
  
ggplot(polygons, aes(area)) +
  geom_freqpoly() +
  labs(x="Surface", y=NULL, title=paste(target, "(Source : OpenStreetMap)")) +
  theme_light()

ggplot(polygons, aes(area)) +
  geom_histogram(binwidth = 100) +
  labs(x="Surface", y=NULL, title=paste(target, "(Source : OpenStreetMap)")) +
  theme_light()

References