Data Visualization In R With plotly

Words
304
Reading
2 min
Listen
Play
9y

Hi there. In this post, I showcase some plots in R with the plotly package. I have been using this plotly cheatsheet as and their website for R as references.

Although I use ggplot2 a lot in R, I think that plotly has some good features and the plots look pretty clean for the most part.


Featured Image

Plots


  • Scatterplot
  • Line Plot
  • Area Plot
  • Bar Chart
  • Histogram
  • Box Plots
  • 3D Scatterplot

To install plotly into R/RStudio use the code install.packages("plotly"). After installation, use library(plotly) to load in the package into R/RStudio.

The main function for plotting in plotly is plot_ly().

Scatterplot

Generating a scatterplot is not too difficult. I think adding in labels and a title can be somewhat tricky as it takes time to get through some of the documentation.

# Scatter Plot

plot_ly(x = c(2, 3, 5, 8, 10), y = c(1, 0, 4, 2, 8), type = "scatter", mode = 'markers') %>%
  layout(xaxis = list(title = "\n x"), 
         yaxis = list(title = "y \n"),
         title = "Simple Scatterplot \n")

scatterplot.png

Line Plot

A line plot is basically a scatterplot with a line(s) going through the points.

# Line Plot:

plot_ly(x = c(2, 3, 5, 8, 10), y = c(1, 0, 4, 2, 8), type = "scatter", mode = "lines") %>%
  layout(xaxis = list(title = "\n x"), 
         yaxis = list(title = "y \n"),
         title = "Simple Line Plot \n")

linePlot.png

Area Plot

A further extension would be adding a filled area under the line (curve).

# Area Plot (Area Under A Line/Curve)

plot_ly(x = c(2, 3, 5, 8, 10), y = c(1, 0, 4, 2, 8), type = "scatter", mode = "lines", 
        fill = 'tozeroy')

areaPlot.png

Bar Graph

In the plotly bar graph, you need to input values for the horizontal axis and the counts in the vertical axis. I have changed the opacity to 0.5 to have the blue bars be lighter.

# Bar Chart (Fake Survey):

plot_ly(x = c("Yes", "No"), y = c(54, 60), type = "bar", opacity = 0.5,
        marker = list(color = 'rgb(158,202,225)',
        line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
  layout(xaxis = list(title = "\n Answer"), 
         yaxis = list(title = "Counts \n"),
         title = "Bar Graph Example \n")

barGraph.png

Histogram

In this histogram example, I simulate/generate/sample 10000 standard normal random variables and plot the results in a histogram. The resulting histogram approximates the standard normal distribution density (bell shaped curve).

# Histogram:

norm_rv <- rnorm(n = 10000, mean = 0, sd = 1)

plot_ly(x = norm_rv, type = "histogram") %>%
  layout(xaxis = list(title = "\n Value"), 
         yaxis = list(title = "Counts \n"),
         title = "Histogram Of Simulated Standard Normal Random Variables  \n")

histNorms.png

Box Plots

# Box Plot:

plot_ly(y = rnorm(100), type = "box")

boxPlot01.png

The add_trace() function allows for an additional box plot. The second box plot is for chi-squared random variables. (A chi-squared random variable is the square of a normal random variable.)

plot_ly(y = rnorm(100), type = "box") %>%
  add_trace(y = rchisq(n = 100, df = 1, ncp = 0)) # Two box plots

boxPlot02.png

3D Scatterplot

You can create three dimensional scatter plots in plotly by having the type as scatter3d and having x, y and z. In my computer and RStudio, I found it hard to play with the 3D output. The image below could be better.

# 3D Scatter Plot:

plot_ly(x = rnorm(10), y = rnorm(10), z = rnorm(10),
        type = "scatter3d",
        mode = "markers")

scatterplot3d.png


Edit: Fixed a few typos.

Data Visualization In R With plotly | Ecency