PLotly Project

G. Jan

19 July 2018

Instructions

Create a web page using R Markdown that features a map created with Plotly.

Host your webpage on either GitHub Pages, RPubs, or NeoCities.

Map

Here is the coding using leaflet and the output map showing the 5 biggest cities in Norway with their inhabitants:

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
#  download.file("https://www.ssb.no/eksport/tabell.csv?key=222368","~/Downloads/cities.csv")
  cities <- read.csv("~/Downloads/cities.csv",sep = ";") 
  cities <- cities[10:14,1:2]
  cities[,3] <- c(10.7522,5.324383,5.7331,10.3951,10.2045)
  cities[,4] <- c(59.9139,60.397076,58.9700,63.4305,59.7441)
  names(cities) <- c("City","Population","Longitude","Latitude")
  rownames(cities) <- NULL
  cities$City <- as.character(cities$City)
  cities$Population <- as.numeric(as.character(cities$Population))
  
  
  g <- list(
  scope = 'europe',
  projection = list(type = 'mercador'),
  showland = TRUE,
  landcolor = toRGB("gray85"),
  subunitwidth = 1,
  countrywidth = 1,
  subunitcolor = toRGB("white"),
  countrycolor = toRGB("white")
)

p <- plot_geo(cities) %>%
  add_markers(
    x = ~Longitude, y = ~Latitude, 
    size = ~Population/50000, 
    #hoverinfo = "text",
    text = ~paste(as.character(Population),"inhabitants -",City)
  ) %>%
  layout(title = 'Top 5 Cities in Norway - Population', geo = g)
p