- 1
- grammar of data manipulation https://dplyr.tidyverse.org/articles/dplyr.html
- 2
- data package
1 animation
show
<- c("India", "China", "United States", "Indonesia", "Pakistan", "Nigeria", "Brazil", "Bangladesh", "Russian", "Federation", "Mexico")
country %>%
countrypops filter(country_name %in% country) %>%
plot_ly(
x = ~country_name,
y = ~population,
frame = ~year,
type = 'bar',
mode = 'markers',
color = ~population,
showlegend = F
%>%
) animation_button(
x = 0, xanchor = "right", y = 10^6, yanchor = "bottom"
%>%
) animation_opts(
transition = 10,
frame = 20) %>%
animation_slider(
currentvalue = list(prefix = "YEAR ", font = list(color="red"))
%>%
) layout(title = TeX("\\text{evolution de la population des 10 pays les plus peuplé du monde de 1960 à 2022}")) %>%
config(mathjax = "cdn")
show
<- gapminder
df <- df %>%
fig plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
frame = ~year,
text = ~country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
)<- fig %>% layout(
fig xaxis = list(
type = "log"
)
)
fig
3 Fonctions
3.1 plot_ly
: Initiate a plotly visualization
plot_ly(
data = data.frame(),
..., attributes for a giventype
type = c("scatter", "bar", "box", ...)
name,
color,
colors = NULL,
alpha = NULL,
stroke,
strokes = NULL,
alpha_stroke = 1,
size,
sizes = c(10, 100),
span,
spans = c(1, 20),
symbol,
symbols = NULL,
linetype,
linetypes = NULL,
split : (Discrete) values used to create multiple traces (one trace per value).
frame : (Discrete) values used to create animation frames.
width = NULL,
height = NULL,
source = "A"
)
plot_ly() tries to create a sensible plot based on the information you give it. If you don’t provide a trace type, plot_ly() will infer one.
show
plot_ly(economics, x = ~pop)
plot_ly(economics, x = ~date, y = ~pop)
plot_ly() doesn’t require data frame(s), which allows one to take advantage of trace type(s) designed specifically for numeric matrices
show
plot_ly(z = ~volcano)
plot_ly(z = ~volcano, type = "surface")
plotly has a functional interface: every plotly function takes a plotly object as it’s first input argument and returns a modified plotly object
show
plot_ly(economics, x = ~date, y = ~unemploy/pop)
add_lines(plot_ly(economics, x = ~date, y = ~unemploy/pop))
To make code more readable, plotly imports the pipe operator from magrittr
show
%>% plot_ly(x = ~date, y = ~unemploy/pop) %>% add_lines() economics
Attributes defined via plot_ly() set ‘global’ attributes that are carried onto subsequent traces, but those may be over-written
show
plot_ly(economics, x = ~date, color = I("black")) %>%
add_lines(y = ~uempmed) %>%
add_lines(y = ~psavert, color = I("red"))
Attributes are documented in the figure reference -> https://plotly.com/r/reference You might notice plot_ly() has named arguments that aren’t in this figure reference. These arguments make it easier to map abstract data values to visual attributes.
show
<- plot_ly(palmerpenguins::penguins, x = ~bill_length_mm, y = ~body_mass_g)
p add_markers(p, color = ~bill_depth_mm, size = ~bill_depth_mm)
add_markers(p, color = ~species)
add_markers(p, color = ~species, colors = "Set1")
add_markers(p, symbol = ~species)
3.2 add_trace
: Add trace(s) to a plotly visualization
add_trace
add_markers
add_text
add_paths
add_lines
add_segments
add_polygons
add_sf
add_table
add_ribbons
add_image
add_area
add_pie
add_bars
add_histogram
add_histogram2d
add_heatmap
add_contour
add_surface
add_histogram2dcontour
add_mesh
add_scattergeo
add_boxplot
add_choroplet
scatter trace with mode of text
show
<- plot_ly(economics, x = ~date, y = ~uempmed)
p add_text(p, text = "*")
scatter trace with mode of lines
show
add_paths(p)
like add_paths()
, but ensures points are connected according to x
show
add_lines(p)
if you prefer to work with plotly.js more directly, can always use add_trace()
and specify the type yourself
show
add_trace(p, type = "scatter", mode = "markers+lines")
plot_ly(economics, x = ~date, y = ~uempmed, color = I("red"), showlegend = FALSE) %>%
add_lines() %>%
add_markers(color = ~pop)
mappings provided to plot_ly()
are “global”, but can be overwritten
show
# a number of `add_*()` functions are special cases of the scatter trace
plot_ly(economics, x = ~date) %>%
add_ribbons(ymin = ~pce - 1e3, ymax = ~pce + 1e3)
use group_by()
(or group2NA()
) to apply visual mapping once per group (e.g. one line per group)
show
%>%
txhousing group_by(city) %>%
plot_ly(x = ~date, y = ~median) %>%
add_lines(color = I("black"))
show
plot_ly(mtcars, x = ~factor(vs), y = ~mpg) %>%
add_boxplot()
plot_ly(mtcars, x = ~factor(vs), y = ~mpg) %>%
add_trace(type = "violin")
show
%>%
mtcars plot_ly(x = ~factor(vs)) %>%
add_histogram()
%>%
mtcars ::count(vs) %>%
dplyrplot_ly(x = ~vs, y = ~n) %>%
add_bars()
add_histogram()
does binning for you
but you can ‘pre-compute’ bar heights in R
show
<- plot_ly(geyser, x = ~waiting, y = ~duration))
(p add_histogram2d(p)
add_histogram2dcontour(p)
the 2d analogy of add_bars() is add_heatmap()/add_contour()
show
<- kde2d(geyser$waiting, geyser$duration)
den <- plot_ly(x = den$x, y = den$y, z = den$z)
p add_heatmap(p)
add_contour(p)
add_table()
makes it easy to map a data frame to the table trace type
show
plot_ly(economics) %>%
add_table()
pie charts!
show
<- data.frame(labels = c("A", "B", "C"), values = c(10, 40, 60))
ds plot_ly(ds, labels = ~labels, values = ~values) %>%
add_pie() %>%
layout(title = "Basic Pie Chart using Plotly")
show
data(wind)
plot_ly(wind, r = ~r, theta = ~t) %>%
add_area(color = ~nms) %>%
layout(
polar = list(
radialaxis = list(ticksuffix = "%"),
angularaxis = list(rotation = 90)
) )
for more, see https://plotly.com/r/animating-views.html