Getting Started

First, you will need to install and load the Plotly package. You will also need to load the dataset that you will be visualizing. For this codethrough, we will examine the functionality of the Plotly package using the Starwars dataset.

Preview of Starwars Dataset

head (starwars)

What is Plotly?

Plotly is a graphing library that R users can use to create a variety of different types of data visualizations. In this course, we have mainly used ggplot for data visualizations, and while there are many similarities between ggplot and Plotly, there are some key differences that set them apart.

Why use Plotly?

The key question here is why and in what context we might choose Plotly over other similar data visualization packages. The main advantage of using Plotly seems that be that the graphs produced by Plotly are interactive, while ggplot and other similar packages usually produce static graphs. Graphs created using Plotly allow users to zoom, scale, select data points, etc. within the graph. Plotly is wed-based, and users can share their data visualizations online after creating an account at the Plotly.

We can use Ploty to create a wide variety of data visualizations including:

  • Line Plots
  • Bar Charts
  • Pie Charts
  • Bubble Charts
  • Choropleth Maps
  • Contour Plots
  • And Many More!

In this brief codethrough, we will look at three basic plots that can be created using Ploty is just a few quick and easy steps. We will also explore tricks that can be used for formatting and stylizing plots and text for our Plotly visualizations.

Bar Graph: Visualizing Data for Multiple Categorical Groups

The following example demonstrates how to create a basic bar graph in Plotly using the starwars dataset. This visualization shows the number of female characters in the starwars films by species.

p1 <- plot_ly(starwars, type='bar', x = ~species, y = ~sex)

p1

And we can easily clean this bar chart up by taking away the gridlines and adding a title as shown below.

p1 <- plot_ly(starwars, type='bar', x = ~species, y = ~sex)

p1 <- p1 %>% layout(title = 'Female Star Wars Charaacters by Species',
         xaxis = list(showgrid = FALSE),
         yaxis = list(showgrid = FALSE),
         showlegend = FALSE)
p1

The following scatterplot depicts the distribution of height and mass of the Star Wars characters. We have own extreme outlier here skewing the data. Can you guess who it is? Hover over the outlier data point to read the name.

Scatterplot: Visualizing Continuous Data

p2 <- plot_ly(starwars, x = ~mass, y = ~height, color = ~gender, text = ~name, type = "scatter")

p2

We can also quickly add annotation to bring attention to notable trends in the data.

p2 <- p2 %>% add_annotations(
    x=1358,
    y=178,
    xref = "x",
    yref = "y",
    text = "Outlier: Extremely High Mass",
    showarrow = T,
    arrowhead = 4,
    arrowsize = .5)
p2

Learn More about Plotly

For more information about Ploty, check out these sources.

References

The following works were referenced in the making of this RColorBrewer code through: