This document shows how plotly can help statistics and data scientists interactively select or deselect data, filter or zoom it, primarily when they work with large or dense data.
Plotly is a strength visualization package because it creates highly interactive and visually appealing charts, bars and graphs.
In this vignette, we will start with an introduction of plotly then we will examine the features of plotly charts as bellow;
According to this website, plotly is an open-source R package for creating interactive web-based graphs via the open-source JavaScript graphing library plotly.js. Plotly is free and can be used offline, without being connected to the internet [2].
There are two ways for downloading plotly:
Plotly package can be easily installed in CRAN by using install.package() function and loaded from library().
install.packages("plotly")
library("plotly")
Plotly can install from GitHub through the devtools R package.
devtools::install_github("ropensci/plotly")
To create basic graphs, we use the Salaries dataset from this website.
#> # A tibble: 397 x 6
#> rank discipline yrs.since.phd yrs.service sex salary
#> <chr> <chr> <dbl> <dbl> <chr> <dbl>
#> 1 Prof B 19 18 Male 139750
#> 2 Prof B 20 16 Male 173200
#> 3 AsstProf B 4 3 Male 79750
#> 4 Prof B 45 39 Male 115000
#> 5 Prof B 40 41 Male 141500
#> 6 AssocProf B 6 6 Male 97000
#> 7 Prof B 30 23 Male 175000
#> 8 Prof B 45 45 Male 147765
#> 9 Prof B 21 20 Male 119250
#> 10 Prof B 18 18 Female 129000
#> # ... with 387 more rows
In this vignette, we want to show two different approaches for creating Plotly:
Plot_ly() is a function of the plotly package which delivers a direct interface to plotly [1]. It has various arguments such as color, type, size, symbol and e.g.
Plot_ly(data, x, y, type, mode, color, size,…)
data A data frame
X, Y Axis values
type Specifies the type of plot such as scatter, bar, histogram, heatmap, box plots e.g.
Color The colour of data point, use the function of I() to avoid scaling
Size Specifies the size of data points, use via I() to avoid altogether (e.g., size = I(30)).
There are two categories for describing plotly’s attributes:
- traces (describe a single series of data in a graph)
- layout (apply to the rest of the graph, e.g. title, xaxis, yaxis)
The following example shows how trace_add() and layout() are inlined with links to each attributes reference section.
Plot_ly(data, x, y, type, mode, color, size,….) %>%
add_trace(x, y, mode, list, color) %>%
layout(title, xaxis, yaxis)
Figure 1 shows the basic scatter plot by using the plot_ly function.
pal <- c("darkolivegreen3", "dodgerblue3", "deeppink3")
fig <- plot_ly(data = Salaries, x= ~yrs.service, y= ~salary, color = ~rank, colors = pal)
figFigure 1: Interactive scatter plot by plot_ly
By clicking on legend entries, we can simply download the plot as a png, hide or show traces, click-and-drag on the chart to zoom in or zoom out, double-click to autoscale, shift-and-drag to the pan. Also, we can select and deselect university ranks on the graph.
Figure 2 shows the basic interactive histogram. As codes show, we can have a histogram graph by adding type = “histogram” argument. If we want to have other graph types, we need to change the type attribute input.
pal = c("deeppink3","dodgerblue3")
axx <- list( title = "Years")
axy <- list(title = "Amount")
fig <- plot_ly(data = Salaries, x= ~yrs.service, type = "histogram")
fig <- fig %>% add_trace( type="histogram", x=~yrs.since.phd)
figFigure 2: Interactive histogram by plot_ly
The histogram of “yrs.since.phd” added to the existing histogram by inputting add_trace().
Figure 3 shows the 3D interactive graph. In this example, we set axes names by linking layout().
axx <- list( title = "Years.service")
axy <- list(title = "Salary")
axz <- list(title = "Rank")
fig <- plot_ly(data = Salaries, x= ~yrs.service, y= ~salary, z = ~rank, type = 'mesh3d', aspectmode='data')
fig <- fig %>% layout(scene = list(xaxis=axx,yaxis=axy,zaxis=axz))
figFigure 3: Interactive 3D Plot_ly
ggplotly() is a function which is converted ggplot2 graphs to interactive plotly objects. It is a quick way for adding interactivity to existing ggplot2 charts [1].
ggplotly(p, width = NULL, height = NULL, dynamicTicks = FALSE, layerData = 1, originalData = TRUE, source = "A", ...)
p ggplot object
width Width of the plot (optional).
height Height of the plot (optional).
Argument p is mandatory, but other arguments are optional. For more information about other arguments, please visit this website.
P <-ggplot(data = Salaries, aes(x= yrs.service, y= salary))+ geom_point(aes(color = rank)) + geom_smooth(method = "lm", se = FALSE)+ labs(x="years of service",y="Salary") + theme_classic()
ggplotly(P)