March 29, 2019

Objectives

The Shiny app has been developed to comply with the requirements of JHU Coursera course 9 week 4 assignment:

  • Develop a Shiny app that includes reactive output
  • Deploy it to Rstudio´s Shiny server
  • Write documentation for users of the app and post it at the Shiny website
  • The presentation must contain some embedded R code that gets run when slidifying the document
  • Do all this in a "simple project given the short time frame"" (Note by TheCmos: In my humble opinion the instructions for this assignment are quite confusing, at least for a non English native speaker. I hope I got them right).

App purpose

This app reactively plots, on a simple chart, the latitude and longitude of World capitals within a population range that is selected by the user through an input slider.

Dot sizes are proportional to population of the cities plotted. City count and capital names within the population range are listed below the chart.

The app chart is very simple and it includes a World map obtained from the 'rnaturalearth' package.

Source of the dataset: https://simplemaps.com/data/world-cities, updated as of August 2018 according to the authors.

The dataset is loaded into R, adapted to the European settings of TheCmos' computer (',' vs '.', …) and the ggplot2 package is used to create the graph.

Code example

An example of the code used for the main graph is presented below, and an example of the graph is on the next slide.

library(dplyr);library(ggplot2);library(rnaturalearth)
minX <- 4000000;maxX <- 6000000
worldcities<-read.csv("worldcities_comma.csv",sep=";")
wc<-worldcities;wc$lat<-gsub(",",".",wc$lat);wc$lng<-gsub(",",".",wc$lng)
wc<-transform(wc,lat=as.numeric(lat),lng=as.numeric(lng))
wccap<-filter(wc,capital=="primary")
caps<-filter(wccap,population>minX & population<maxX)
xlab <- "Longitude";ylab <- "Latitude";main <- "Latitude and Longitude of the World Capitals within the selected population range";leg<- "right";lab_sz<- 4

spdf_world <- ne_countries()
world<-fortify(spdf_world)
    
g<-ggplot(world,aes(long,lat))
g<- g+geom_polygon(aes(group = group), colour = "grey50", fill = NA) +
coord_quickmap()
g<-g+theme_bw()
g<-g+geom_point(data=caps,mapping=aes(x=lng,y=lat,color=city,size=population,alpha=1/3))
g<-g+theme(legend.position="none") 
g<-g+ scale_x_continuous(limits = c(-180, 180))+scale_y_continuous(limits = c(-90, 90))+ labs(x=xlab,y=ylab,title=main,subtitle="Source: https://simplemaps.com/data/world-cities , as of August 2018")+ guides(color=guide_legend("Country"))
g<-g+theme(plot.title = element_text(hjust = 0.5,size=20.25)) +theme(plot.subtitle=element_text(size=16, hjust=0.5, face="italic", color="blue"))
g<-g+geom_text(data=caps,aes(x=lng,y=lat,label=city
    ) ,hjust=0.5, vjust=-1,size=lab_sz
)

Plot example