library(tidyverse) library(plotly) library(gapminder)
The packages used are tidyverse (dplyr), for filtering, plotly, for plotting, and gapminder for general some country data.
1/7/2021
library(tidyverse) library(plotly) library(gapminder)
The packages used are tidyverse (dplyr), for filtering, plotly, for plotting, and gapminder for general some country data.
We filtered in the dataframe the countries of Europe and we put some features (xlab, ylab and a title) to a line plot, for time series.
data(gapminder)
europe <- gapminder %>%
filter(continent=="Europe")
x <- list(title = "Year")
y <- list(title = "GDP per capita")
p<-plot_ly(europe, x = ~year, y = ~gdpPercap, color = ~country,
mode="lines") %>%
layout(title = "GDP per capita in European countries (1952 - 2007)",
xaxis = x, yaxis = y,
annotations = list(x = 0, y = -0.1, text = "Source: GAPMINDER",
showarrow = F, xref='paper', yref='paper',
xanchor='left', yanchor='auto', xshift=0, yshift=0,
font=list(size=15, color="blue")))
gapm2007 <- gapminder %>% filter(year== 2007)
x <- list(title = "GDP/capita")
y <- list(title = "Life expectancy")
q <- plot_ly(gapm2007, 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,2),'</br>',
"Population:", format(pop, big.mark="."))) %>%
layout(title = "World: GDP/capita, life expectancy and population in 2007",
xaxis = x, yaxis = y,
annotations = list(x = 0, y = -0.1, text = "Source: GAPMINDER",
showarrow = F, xref='paper', yref='paper',
xanchor='left', yanchor='auto', xshift=0, yshift=0,
font=list(size=15, color="blue")))