Fisseha Berhane, PhD

Data Scientist at Aurotech

443-970-2353 fisseha@jhu.edu CV Resume Linkedin GitHub twitter twitter

How Many Live on How Much, and Where with Shiny





server.R

In [178]:
library(shiny)
library(rgdal)    # working with shapefile
library(dplyr)    # awesome data manipulation
library(httr)     # getting data
library(tidyr)    # reshaping data
library(ggplot2)  # plotting
library(ggthemes) # theme_map
library(gridExtra)

data = read.csv("http://www.pewglobal.org/wp-content/themes/pew-global/interactive-global-class.csv")
colnames(data)=make.names(colnames(data))
data$id=data$name

try(invisible(GET("http://www.pewglobal.org/wp-content/lib/js/world-geo.json",
                  write_disk("world-geo.json"))), silent=TRUE)

#  use ogrListLayers("world-geo.json") to see file type & 
#  layer info to use in the call to readOGR
 
world <- readOGR("world-geo.json", "OGRGeoJSON")
 world_wt <- spTransform(world, CRS("+proj=robin"))
 worldmap <- fortify(world_wt)

worldmap %>%
  left_join(data_frame(id=rownames(world@data), name=world@data$name)) %>%
  select(-id) %>%
  rename(id=name) -> worldmap

world_map = merge(worldmap, data,by ="id",all.x = TRUE)
colnames(world_map)=c("id", "long", "lat", "order", "hole", "piece","group","name","poor", "low", "middle", "upper_middle", "high", "change_poor", "change_low", "change_middle", "change_upper_middle", "change_high" )

# Reorder the data
world_map = world_map[order(world_map$group, world_map$order),]


shinyServer(function(input, output) {
    
    which_income <- reactive({
        if(input$income %in% c("Poor", "Low", "Middle", "Upper_Middle", "High")) {
            tolower(input$income)
        }
    })
    
    change <- reactive({
        if(input$change%in% c("Change_Poor", "Change_Low", "Change_Middle", "Change_Upper_Middle", "Change_High")) {
            tolower(noquote(input$change))
        }
    })
    
    output$myplot <- renderPlot({
       
        
           if(input$income=="Poor"){
           
               g1<-ggplot(world_map, aes(x=long, y=lat, group=group))+
                   geom_polygon(aes(fill=poor), color="gray70") 
           }
        
        else if(input$income=="Low"){
            
            g1<-ggplot(world_map, aes(x=long, y=lat, group=group))+
                geom_polygon(aes(fill=low), color="gray70") 
        }
        
        else if(input$income=="Middle"){
            
            g1<-ggplot(world_map, aes(x=long, y=lat, group=group))+
            geom_polygon(aes(fill=middle), color="gray70") 
        }
        
        
        else if(input$income=="Upper_Middle"){
            
            g1<-ggplot(world_map, aes(x=long, y=lat, group=group))+ 
            geom_polygon(aes(fill=upper_middle), color="gray70") 
        }
        
        else if(input$income=="High"){
            
            g1<-ggplot(world_map, aes(x=long, y=lat, group=group))+
            geom_polygon(aes(fill=high), color="gray70")
        }
        
      if(input$change=="Change_Poor"){
            
            g2<-ggplot(world_map, aes(x=long, y=lat, group=group)) +
                geom_polygon(aes(fill=change_poor), color="gray70") 
        }
        
        
        else if(input$change=="Change_Low"){
            
            g2<-ggplot(world_map, aes(x=long, y=lat, group=group)) +
                geom_polygon(aes(fill=change_low), color="gray70") 
        }
        
        else if(input$change=="Change_Middle"){
            
            g2<-ggplot(world_map, aes(x=long, y=lat, group=group)) +
                geom_polygon(aes(fill=change_middle), color="gray70") 
        }
        
        else if(input$change=="Change_Upper_Middle"){
            
            g2<-ggplot(world_map, aes(x=long, y=lat, group=group)) +
                geom_polygon(aes(fill=change_upper_middle), color="gray70") 
        }
        
        else if(input$change=="Change_High"){
            
            g2<-ggplot(world_map, aes(x=long, y=lat, group=group)) +
                geom_polygon(aes(fill=change_high), color="gray70") 
        }
        
        
    
        if (exists("g1") & exists("g2")){
            g1<-g1+theme(axis.text.y   = element_blank(),
                         line = element_blank(),
                         axis.text.x   = element_blank(),
                         axis.title.y  = element_blank(),
                         axis.title.x  = element_blank(),
                         panel.background = element_blank(),
                         panel.grid.major = element_blank(), 
                         panel.grid.minor = element_blank(),
                         panel.border = element_rect(colour = "gray70", fill=NA, size=0.5))+
                       theme(aspect.ratio=0.4,legend.key.size = unit(1.5, "cm"),
                      legend.title = element_text(size = 15, colour = "blue"),
                      legend.title.align=0.3,legend.text = element_text(size = 12))+scale_fill_gradient(low = "#C2E0C2", high = "#1A4C1A", guide = "colorbar",na.value="white")
            
            g2<-g2+theme(axis.text.y   = element_blank(),
                         line = element_blank(),
                         axis.text.x   = element_blank(),
                         axis.title.y  = element_blank(),
                         axis.title.x  = element_blank(),
                         panel.background = element_blank(),
                         panel.grid.major = element_blank(), 
                         panel.grid.minor = element_blank(),
                         panel.border = element_rect(colour = "gray70", fill=NA, size=0.5))+theme(aspect.ratio=0.4,legend.key.size = unit(1.5, "cm"),
                         legend.title = element_text(size = 15, colour = "blue"),
                         legend.title.align=0.3,legend.text = element_text(size = 12))+scale_fill_gradient(low = "#C2D6D6", high = "#660033", guide = "colorbar",na.value="white")
            g3<-grid.arrange(g1, g2, nrow=2)
        }
        
        else if (exists("g1") & !exists("g2")){
            g3<-g1+theme(axis.text.y   = element_blank(),
                      line = element_blank(),
                      axis.text.x   = element_blank(),
                      axis.title.y  = element_blank(),
                      axis.title.x  = element_blank(),
                      panel.background = element_blank(),
                      panel.grid.major = element_blank(), 
                      panel.grid.minor = element_blank(),
                      panel.border = element_rect(colour = "gray70", fill=NA, size=0.5))+
                
                      theme(aspect.ratio=0.4,legend.key.size = unit(1.5, "cm"),
                         legend.title = element_text(size = 15, colour = "blue"),
                         legend.title.align=0.3,legend.text = element_text(size = 12))+scale_fill_gradient(low = "#C2E0C2", high = "#1A4C1A", guide = "colorbar",na.value="white")
        }
        
        else if (!exists("g1") & exists("g2")){
            g3<-g2+theme(axis.text.y   = element_blank(),
                         line = element_blank(),
                         axis.text.x   = element_blank(),
                         axis.title.y  = element_blank(),
                         axis.title.x  = element_blank(),
                         panel.background = element_blank(),
                         panel.grid.major = element_blank(), 
                         panel.grid.minor = element_blank(),
                         panel.border = element_rect(colour = "gray70", fill=NA, size=0.5))+theme(aspect.ratio=0.4,legend.key.size = unit(1.5, "cm"),
                         legend.title = element_text(size = 15, colour = "blue"),
                         legend.title.align=0.3,legend.text = element_text(size = 12))+scale_fill_gradient(low = "#C2D6D6", high = "#660033", guide = "colorbar",na.value="white")
        }
        if(exists("g3")) g3
    })
    
    
    
    output$help <- renderText({
        if(input$reference==T){
            'Please, visit this link for details:
            http://www.pewglobal.org/interactives/global-population-by-income/'
        }})
    
            
})


ui.R

In [214]:
# start user interface ----
library(shiny)
library(shinydashboard)

dashboardPage(
dashboardHeader(title="By Fisseha Berhane"),

dashboardSidebar(
        
                     br(),
                     br(),
                     br(),
                    
                     selectInput("income", 
                                 label = tags$h4(strong(em("Select Income",style="color:#FFA319;font-size:120%"))),
                                 choices = c("Poor", "Low", "Middle", "Upper_Middle", "High","--"),
                                 selected = "--"),
                     
                     br(),
                     br(),
                     br(),
                     
                     selectInput("change", 
                                 label = tags$h4(strong(em("Percentage Change",style="color:#FFA319;font-size:120%"))),
                                 choices = c("Change_Poor", "Change_Low", "Change_Middle", "Change_Upper_Middle", "Change_High","--"),
                                 selected = "--"),
                     
                
                     
                   
                     br(),
                     br(),
                     br(),
                     br(),
                     checkboxInput("reference", 
                                   label = strong("Help",style="color:#FFA319;font-size:100%")),
                     br(),
                     p(textOutput("help")),
                     br(),
                     br()
                     
                     
        ),
        
        
        #####
        ##  Main Panel
        #### help ====        
dashboardBody(    
fluidRow(
            h5(em(strong("How Many Live on How Much, and Where", style="color:darkblue;font-size:210%")),align = "center"),
            
            plotOutput("myplot", height = 600)
           
        )
        ))




comments powered by Disqus