Chapter 3 Statistical Charts

3.1 2D Histograms

A 2D histogram is a visualization of a bivariate distribution. And it looks like a heat map.

#
library(plotly)

s <- matrix(c(1, -.75, -.75, 1), ncol = 2)
obs <- mvtnorm::rmvnorm(500, sigma = s)
fig <- plot_ly(data = mtcars, x = ~hp, y= ~mpg)
fig2 <- subplot(
  fig %>% add_markers(alpha = 0.2),
  fig %>% add_histogram2d()
)

fig2

3.2 Box Plot

3.2.1 Basic Boxplot

library(plotly)
fig <- plot_ly(data = mtcars, y= ~mpg, type = "box")

fig

3.2.2 Horizontal Boxplot

library(plotly)
fig <- plot_ly(data = mtcars, x= ~mpg, type = "box")

fig

3.2.3 Adding Jittered Points

library(plotly)
fig <- plot_ly(data = mtcars, y= ~mpg, type = "box", boxpoints = "all", jitter = 0.3)

fig

3.2.4 Several Box Plots

fig <- plot_ly(data = mtcars, y= ~mpg, color = ~as.factor(cyl), type = "box")

fig

3.3 Histograms

3.3.1 Basic Histograms

library(plotly)
fig <- plot_ly(data = mtcars, x = ~hp, type = "histogram", nbinsx = 15)

fig

3.3.2 Stacked Histogram


fig <- plot_ly(data = mtcars, x = ~hp,
             type = "histogram",
             cumulative = list(enabled=TRUE),
             nbinsx = 15)

fig

3.4 Violin Plots

fig <- mtcars %>%
  plot_ly(
    y = ~hp,
    type = 'violin',
    box = list(
      visible = T
    ),
    meanline = list(
      visible = T
    ),
    x0 = 'Horse Power'
  ) 

fig <- fig %>%
  layout(
    yaxis = list(
      title = "",
      zeroline = F
    )
  )

fig