Chapter 54 Plotly R graphing

Brian Mao

54.1 Introduction

Plotly’s R graphing library makes interactive, publication-quality graphs. in this into to Plotly I will show some examples of how to make line plots, scatter plots, area charts, bar charts, box plots, histograms, heatmaps, and 3D scatter plot charts. there are a lots more advanced function in the Plotly. you may explore it later on yourself

54.3 Install and packages

library(plotly)
library(ggplot2)

54.4 Basic examples

54.4.1 Basic Scatter Plot

library(plotly)
fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig

54.4.2 Plotting Markers and Lines

library(plotly)
trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)
data <- data.frame(x, trace_0, trace_1, trace_2)
fig <- plot_ly(data, x = ~x)
fig <- fig %>% add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines')
fig <- fig %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers')
fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
fig

54.4.3 Data with different Symbols

library(plotly)
fig <- 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))
fig

54.5 Statistical Charts

54.5.1 Basic Boxplot

library(plotly)
fig <- plot_ly(y = ~rnorm(100), type = "box")
fig <- fig %>% add_trace(y = ~rnorm(300, 1))
fig

54.5.2 overlaid Histograms

fig <- plot_ly(alpha = 0.6)
fig <- fig %>% add_histogram(x = ~rnorm(1000))
fig <- fig %>% add_histogram(x = ~rnorm(500) + 1)
fig <- fig %>% layout(barmode = "overlay")
fig

54.5.3 Basic Heatmap

library(plotly)
fig <- plot_ly(z = volcano, type = "heatmap")
fig

54.6 more advanced map plot

54.6.1 Flight Paths Map

library(plotly)

fig <- plot_geo(lat = c(40.7127, 51.5072), lon = c(-74.0059, 0.1275))
fig <- fig %>% add_lines(color = I("blue"), size = I(2))
fig <- fig %>% layout(
    title = 'London to NYC Great Circle',
    showlegend = FALSE,
    geo = list(
      resolution = 50,
      showland = TRUE,
      showlakes = TRUE,
      landcolor = toRGB("grey80"),
      countrycolor = toRGB("grey80"),
      lakecolor = toRGB("white"),
      projection = list(type = "equirectangular"),
      coastlinewidth = 2,
      lataxis = list(
        range = c(20, 60),
        showgrid = TRUE,
        tickmode = "linear",
        dtick = 10
      ),
      lonaxis = list(
        range = c(-100, 20),
        showgrid = TRUE,
        tickmode = "linear",
        dtick = 20
      )
    )
  )

fig

54.6.2 3D Scatter Plot

library(plotly)

mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
mtcars$am <- as.factor(mtcars$am)

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~am, colors = c('#BF382A', '#0C4B8E'))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
                     yaxis = list(title = 'Gross horsepower'),
                     zaxis = list(title = '1/4 mile time')))

fig

54.7 Conclusion

In this project I explored different methods of plotting interactive plot by utilizing plotly and ggplot2. you can explore more about the tool using R, python and Javascript. there are more resource available below. it is a very user friendly tool and the interface is well designed. it allows users to design their own way to tell the story to its audience. effectively utilizing this tool will tremendously help you to improve your data visualization skills

54.8 Work Cited:

Create Interactive Web Graphics via ‘plotly.js’ https://cran.r-project.org/web/packages/plotly/plotly.pdf Author Carson Sievert [aut, cre] (https://orcid.org/0000-0002-4958-2844), Chris Parmer [aut], Toby Hocking [aut], Scott Chamberlain [aut], Karthik Ram [aut], Marianne Corvellec [aut] (https://orcid.org/0000-0002-1994-3581), Pedro Despouy [aut],

https://github.com/plotly https://plotly.com/graphing-libraries/

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.