Andy Grogan-Kaylor
September 02, 2021
There are an ever increasing number of animated, or interactive, graphical libraries for R. Choosing which library to use, and which library will be persistent and stable over time is a challenge.
plotly
is a JavaScript graphing library that creates graphics for the web.
plotly
for R is one of the easiest, and most richly featured libraries to use for creating graphics on the web.
ggplotly()
Remember all ggplots start simple, and then are tweaked…
# NB, I am mapping the plot into an object
# this is a tremendously useful idea, and will come in handy...
p0 <- ggplot(mydata, # the data I am using
aes(x = GDP, # how I am graphing it
y = life_expectancy, # NB I'm 'doubling up'
color = life_expectancy)) + # on dimensions
geom_point() + # the geometries I want to use to graph
geom_smooth()
# this graph needs better labels and titles
p0 # I am replaying the object, i.e. the plot
# NB, I am mapping the plot into an object
# this is a tremendously useful idea, and will come in handy...
p1 <- ggplot(mydata, # the data I am using
aes(x = GDP, # how I am graphing it
y = life_expectancy, # NB I'm 'doubling up'
color = life_expectancy)) + # on dimensions
scale_color_gradient(low = "white",
high = "red") + # CONTINUOUS palette
geom_point() + # the geometries I want to use to graph
geom_smooth()
# this graph needs better labels and titles
p1 # I am replaying the object, i.e. the plot
Pipes %>%
connect pieces of a command e.g. data to a graph command.
plotly
syntaxPlotly Has Similar Logic to ggplot
.
# mapping plot into an object
# to create the graph, connecting 2 components with a pipe
p2 <- mydata %>% # pipes connect the pieces
plot_ly(x = ~GDP, # dimensions of the data we are using
y = ~life_expectancy, # NB the '~'
color = ~life_expectancy, # double counting a dimension
colors = c("red", "blue")) # specifying color
p2 # replay the graph
plotly
does not have every geometry that ggplot2
has, and vice versa.
Most notably, a smoother geometry seems to be missing.
Note also, the cryptic (to me) use of (embedded) lists to style the graph.
p3 <- mydata %>% # pipe connects data to plot
plot_ly(x = ~GDP, # dimensions of the data we are using
z = ~life_expectancy, # NB the '~'
y = ~Gini,
color = ~life_expectancy,
colors = c("red", "blue"),
text = ~country_name) %>% # pipe connects to layout
layout(title = 'Life Expectancy by GDP and Gini',
scene = list(xaxis = list(title = 'GDP'),
yaxis = list(title = 'Gini Inequality Coefficient'),
zaxis = list(title = 'Life Expectancy')))
p3 # replay the graph
On the previous 3D graphs, we had 3 dimensions of GDP, life expectancy and Gini coefficient of inequality.
Do the preceding 3D graph, or the 2D graph on the next slide do a better job of representing multiple dimensions? What are the best ways of representing additional dimensions of data?
p4 <- mydata %>% # pipe connects data to plot
plot_ly(x = ~GDP, # dimensions of the data we are using
y = ~life_expectancy, # NB the '~'
color = ~life_expectancy,
colors = c("red", "blue"),
text = ~country_name) %>% # pipe connects to layout
layout(title = 'Life Expectancy by GDP',
xaxis = list(title = 'GDP'),
yaxis = list(title = 'Life Expectancy'))
p4 # replay the graph
Because plotly
does not have some features of ggplot
, we need to use dplyr
to manage our data (summarize scores) before sending to plotly
.
plotly
relies on ISO standard codes for country names in order to make maps. plotly
can also use State abbreviations, and latitudes and longitudes.
mymap <- mydata %>%
filter(year == 2015) %>% # use dplyr to filter on year
plot_geo(locations = ~iso3c, # ISO3 Country code
color = ~life_expectancy,
z = ~life_expectancy,
text = ~country_name) %>%
layout(title = "Countries by Life Expectancy in 2015",
geo = list(showland = FALSE,
showcountries = TRUE)) %>%
colorbar(title = 'life expectancy')
mymap # replay mymap
myglobe <- mydata %>%
filter(year == 2015) %>%
plot_geo(locations = ~iso3c,
color = ~life_expectancy,
z = ~life_expectancy,
text = ~country_name) %>%
layout(title = "Countries by Life Expectancy in 2015",
geo = list(showland = FALSE,
showcountries = TRUE,
projection = list(type = 'orthographic',
rotation = list(lon = -30,
lat = 10,
roll = 0)))) %>%
colorbar(title = 'life expectancy')
myglobe