URL to interactive

https://danno.shinyapps.io/Ex_9Vis/

URL to Rpubs

http://rpubs.com/fast_Eddie/433458

Objective

Gapminder Use the gapminder package and data to create an interactive data visualisation similar to what Hans Rosling used.

gapminder <- as.data.frame(gapminder)

# str(gapminder)
# head(gapminder, 2)
#View(gapminder)
# Define a function to cap the values outside the limits

cap <- function(x, na.rm = TRUE, ...) {
  qnt <- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
  H <- 1.5 * IQR(x, na.rm = na.rm)
  y <- x
  y[x < (qnt[1] - H)] <- NA
  y[x > (qnt[2] + H)] <- NA
  y
}
gapminder$year <- as.numeric(gapminder$year, na.rm = TRUE) %>% cap()
gapminder$lifeExp <- as.numeric(gapminder$lifeExp, na.rm = TRUE) %>% cap()
gapminder$pop <- as.numeric(gapminder$pop, na.rm = TRUE) %>% cap()
gapminder$gdpPercap <- as.numeric(gapminder$gdpPercap, na.rm = TRUE) %>% cap()
#gapminder$continent <- as.numeric(gapminder$continent)
#gapminder$country <- as.numeric(gapminder$country)
#boxplot(gapminder)
# nrow(gapminder)                 # 1704
n <- gapminder %>% 
  group_by(continent) %>%
  summarise(n_distinct(country))  #distinct(gapminder$continent)
#n

gapminder <-  gapminder %>% group_by(country) %>%  arrange(continent)
gap1 <- gapminder %>% subset(gapminder$year < '1973')
#nrow(gap1)  #710
#gap1

gap2 <- gapminder %>% subset(gapminder$year > '1974' & gapminder$year < '1996')
#nrow(gap2)  #568
#gap2

gap3 <- gapminder %>% subset(gapminder$year > '1996')
#nrow(gap3)  #426
# Global

# Define the UI

ui <- fluidPage(
  
  titlePanel("Gapminder App"),
  
           # revealjs::revealjs_presentation:
           # transition: slide,
  
  # Sidebar with a slider input 
  
  sidebarPanel(
    sliderInput("lifeExp", label = "Year", 
                min = 1950, sep=" ", step = 10, 
                max = 2010, value = 23, 
                
                #min=min(x), max=max(x), value=range(x),
                #step=  10 , #round(diff(range(x))/20, 1), #animate=TRUE,
        
        
                animate = animationOptions(interval = 700, loop = TRUE)),   #,750
                width = 100,
                #animate=animationOptions(100) ,
                
  
  mainPanel(
    plotOutput("scatter"),
      width = 700,
      align = "right",
  
    p("Danno")
  
  )

))


server <- function(input, output, session) {   
  
 
    output$scatter <- renderPlot({                    
      p1 <- ggplot(data = gap1, aes(x = continent, y = gdpPercap, 
                                         color = continent, size = pop))  
      p1 + geom_point()  +
           geom_abline() + 
           geom_smooth() +
           geom_jitter() +
           geom_count()  +
           geom_blank()
    })
      
    output$scatter <- renderPlot({                    
      p2 <- ggplot(data = gap2, aes(x = continent, y = gdpPercap, 
                                         color = continent, size = pop))  
      p2 + geom_point()  +
           geom_abline() + 
           geom_smooth() +
           geom_jitter() +
           geom_count()  +
           geom_blank()
      
    })     
     output$scatter <- renderPlot({                    
      p3 <- ggplot(data = gap3, aes(x = continent, y = gdpPercap, 
                                         color = continent, size = pop))  
      p3 + geom_point()  +
           geom_abline() + 
           geom_smooth() +
           geom_jitter() +
           geom_count()  +
           geom_blank() 
      
    })
  }

    
      ###############################
        



shinyServer(function(input, output) {

    mainPanel(plotOutput("p1"),  #, height=260  
              plotOutput("p2"),  #, height=260
              plotOutput("p2"))   #  height=260))
  
})

# Return a Shiny app object

if (interactive())  shinyApp(ui = ui, server = server)

Conclusion

What I would prefer is a reveal with each step of the slider, which I couldnt find the code for, or rather couldnt find a piece that would work with my coding.

As far as being similar to Hans Rosling, not even close.