Note the use of the p_load() function from the pacman package!
A summary of the average and maximum GDP by continent:
summary<-data%>%
group_by(continent) %>%
summarise(average_gdp=mean(gdpPercap),
max_gdp=max(gdpPercap))
print(summary)
## # A tibble: 5 × 3
## continent average_gdp max_gdp
## <fct> <dbl> <dbl>
## 1 Africa 2194. 21951.
## 2 Americas 7136. 42952.
## 3 Asia 7902. 113523.
## 4 Europe 14469. 49357.
## 5 Oceania 18622. 34435.
Static bubble chart of GDP vs Life Expectancy:
static_plot<-plot_ly(type="scatter", data=data, x=~gdpPercap, y=~lifeExp, size=~pop, text=~country)
static_plot<-static_plot%>%layout(title="GDP vs Life Expectancy",
xaxis=list(title="GDP"),
yaxis=list(title="Life Expectancy"))
static_plot
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: `line.width` does not currently support multiple values.
Animated bubble chart of GDP vs Life Expectancy:
animated_plot<-plot_ly(type="scatter", data=data, x=~gdpPercap, y=~lifeExp, size=~pop, frame=~year, mode="markers", hoverinfo="text", text=~country)
animated_plot<-animated_plot%>%layout(title="GDP vs Life Expectancy",
xaxis=list(title="GDP"),
yaxis=list(title="Life Expectancy"))
animated_plot
animated_chart <- animated_plot %>%
animation_opts(frame = 100,
# This part changed the time between frames from the original
redraw = TRUE,
# This part causes the plot to redraw itself
easing = "linear",
# This part keeps the animation at a constant speed
## autoplay=TRUE
# This part autoplays the animation
)
animated_chart
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
Adding a paused button to the animated plot:
interactive_chart <- animated_chart %>% layout(
updatemenus = list(
list(
type = "buttons",
showactive = FALSE,
x = 0.1,
y = 1.1,
buttons = list(
list(
label = "Pause",
method = "animate",
args = list(NULL, list(frame = list(duration = 0,
redraw = TRUE),
mode = "immediate"))
)
)
)
)
)
interactive_chart
We have already added all titles and hover text, where the hover text shows the country and x/y values.