Plotly in R

In this presentation, it is shown some Plotly examples, to unsderstand the use of this special package.

Plotly is a technical computing company headquartered in Montreal, Quebec, that develops online data analytics and visualization tools. Plotly provides online graphing, analytics, and statistics tools for individuals and collaboration, as well as scientific graphing libraries for Python, R, MATLAB, Perl, Julia, Arduino, and REST.Source

Pie Charts

A pie chart (or a circle chart) is a circular statistical graphic, which is divided into slices to illustrate numerical proportion. *Not to be confused with circle graph. Source

labels = c('Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen')
values = c(4500, 2500, 1053, 500)

fig1 <- plot_ly(type='pie', labels=labels, values=values, 
               textinfo='label+percent',
               insidetextorientation='radial')

Source

fig1

Box Plots

In descriptive statistics, a box plot or boxplot is a method for graphically depicting groups of numerical data through their quartiles. Box plots may also have lines extending from the boxes (whiskers) indicating variability outside the upper and lower quartiles, hence the terms box-and-whisker plot and box-and-whisker diagram. Outliers may be plotted as individual points.Source

library(ggplot2)
fig2 <- plot_ly(ggplot2::diamonds, y = ~price, color = ~cut, type = "box")

Source

fig2

Scatter Plots

A scatter plot is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. If the points are coded (color/shape/size), one additional variable can be displayed. The data are displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis. Source

library(plotly)

fig3 <- plot_ly(data = iris, x = ~Sepal.Length, 
                y = ~Petal.Length, type = 'scatter',
  mode = 'markers', symbol = ~Species, 
  symbols = c('circle','x','o'),
  color = I('black'),
  marker = list(size = 10))

Source

fig3

Bar Charts

A bar chart is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent.

A bar graph shows comparisons among discrete categories. One axis of the chart shows the specific categories being compared, and the other axis represents a measured value.Source.

library(plotly)
library(dplyr)
library(ggplot2)

fig4 <- ggplot2::diamonds
fig4 <- fig4 %>% count(cut, clarity)
fig4 <- fig4 %>% plot_ly(x = ~cut, y = ~n, color = ~clarity)

Source

fig4