Stefano Sanfilippo
2021/07/03
Data are from GAPMINDER: https://gapminder.org.
It's a dataset of 1704 rows and 6 variables:
The app is composed by a plot on the right and a panel and a sidebar panel on the left.
In the sidebar panel we have two dropdowns which allow to select the data to be displayed in the plot:
The plot is a bubble plot. Every observation -corresponding to a country- is a bubble, the position of which in the XY square is fixed by two values:
Every bubble can display a label hovering the mouse pointer on it. The label include 4 values:
This is a shiny app that uses plotly to build its graphics.
ui
server
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("World development indicators (1952 - 2007)"), # Add a title panel
sidebarLayout( # Make the layout a sidebarLayout
sidebarPanel(
selectInput(inputId = "year",
label = "Year",
choice = c(2007,2002,1997,1992,1987,1982,1977,
1972,1967,1962,1957,1952),
selected = 2007),
h5("This dropdown allows you to choose the year for displaying the data. The years are in intervals of 5. The default option is 2007."),
selectInput(inputId = "Continent",
label = "Continent",
choice = c("Africa", "Americas", "Asia", "Europe", "Oceania", "All"),
selected = "All"),
h5("This dropdown allows you to filter the data by continent. The default option is 'All'.") ...
...
),
mainPanel(plotlyOutput("plot1"),
h5("1. The plot is a bubble plot. Every observation -corresponding to a country- is a bubble, the position of which in the XY square is fixed by 'GDP/capita' and 'Life expectancy. The size of the bubble is proportional to the population."),
h5("2. Every bubble can display a label hovering the mouse pointer on it. The label include 4 values: 1) Name of the country; 2) Life expectancy of the country; 3) GDP/capita of the country; 4) Population of the country."))
library(shiny)
library(tidyverse)
library(plotly)
library(gapminder)
server <- shinyServer(function(input, output) {
output$plot1 <- renderPlotly({
if(input$Continent == "All") {
Data <- gapminder %>%
filter(year == input$year)
} else {Data <- gapminder %>%
filter(year == input$year & continent==input$Continent)
}
x <- list(title = "GDP/capita")
y <- list(title = "Life expectancy")
p <- plot_ly(Data, x= ~gdpPercap, y= ~lifeExp, mode="markers",
color = ~country, size= ~pop,
hoverinfo = 'text', ...
... text = ~paste(country,'</br></br>',
"Life expectancy:", round(lifeExp,1),'</br>',
"GDP/Capita:", round(gdpPercap,0),'</br>',
"Population:", format(pop, big.mark=","))) %>%
layout(title = paste("World: GDP/capita, life expectancy and population
year in the year",
input$year,"- Continent:", input$Continent),
xaxis = x, yaxis = y,
annotations = list(x = 0, y = -0.1, text = "Source: GAPMINDER.ORG",
showarrow = F, xref='paper', yref='paper',
xanchor='left', yanchor='auto', xshift=0, yshift=0,
font=list(size=15, color="blue"))) %>%
hide_legend()
}) })