knitr::opts_chunk$set(echo=TRUE, warning=FALSE)
Link: https://rpubs.com/gvt8kv/ICA11
install.packages(“gapminder”)
library(gapminder)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
gdp_summary <- gapminder %>%
group_by(continent) %>%
summarize(avg_gdp = mean(gdpPercap * pop), max_gdp = max(gdpPercap * pop))
print(gdp_summary)
## # A tibble: 5 × 3
## continent avg_gdp max_gdp
## <fct> <dbl> <dbl>
## 1 Africa 20904782844. 4.48e11
## 2 Americas 379262350210. 1.29e13
## 3 Asia 227233738153. 6.54e12
## 4 Europe 269442085301. 2.65e12
## 5 Oceania 188187105354. 7.04e11
install.packages(“plotly”) # Run this once to install Plotly
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
plot_ly(gapminder,
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
type = 'scatter',
mode = 'markers',
marker = list(sizemode = "diameter"))
frame with year as the
variable.animated_plot <- plot_ly(gapminder,
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
frame = ~year,
type = 'scatter',
mode = 'markers',
marker = list(sizemode = "diameter"))
animated_plot
# Frame: This is the time that each framelasts (100 milliseconds)
# Redraw: This is redrawing a new plot each time a new frame is shown
# Easing: This is a trnasiton between frame at a speed constant
animated_chart <- animated_plot %>%
animation_opts(frame = 100,
redraw = TRUE,
easing = "linear")
animated_chart
final_chart <- animated_plot %>%
layout(
title = "Gapminder Data Over Time",
xaxis = list(title = "GDP per Capita"),
yaxis = list(title = "Life Expectancy"),
hovermode = "closest",
updatemenus = list(
list(
type = "buttons",
showactive = FALSE,
x = 1,
y = 0,
xanchor = "right",
yanchor = "bottom",
buttons = list(
list(
label = "Pause",
method = "animate",
args = list(NULL, list(frame = list(duration = 0, redraw = TRUE),
mode = "immediate"))
)
)
)
)
) %>%
plotly::style(
hoverinfo = "text",
text = ~paste("Country:", country, "<br>GDP per Capita:", gdpPercap,
"<br>Life Expectancy:", lifeExp, "<br>Population:", pop)
)
final_chart