Introduction

This code through explores Plotly Library. This package allows us to enhance the disgn of graphs and look at data in different way. Plotly is a powerful tool that allows people to be interactive with the data.


Content Overview

We’ll be looking at base income of MLS soccer player from 2017. Plotly will allow us to see the data in different ways including a scatter plot, a box plot, and a 3D scatter plot.


Example #1

A basic example here will show you how to do a scatter plot. We set the X axis to the club and the Y axis to the base salary. the color will be based on the position.Then we set the marker position and the title and lables:

# Load mls dataset

mls_salaries_2017<-read.csv("C:/Users/Q/Downloads/mls-salaries-2017.csv")

mls <- mls_salaries_2017


#Scatter Plot with Colors
plot_ly(mls, x = ~club, y = ~base_salary, color = ~position,
        type = "scatter", mode = "markers", 
        marker = list(size = 10, opacity = 0.8)) %>%
  layout(title = "Scatter Plot of Position vs Base Salary", 
         xaxis = list(title = "Club"),
         yaxis = list(title = "Base Salary"))

Example #2

Another example is using the data to make bar graph. We can keep the data the same but the type would change to “bar” and the mode would change to “stack”. This would give us a bar graph visual:

#Bar Graph
plot_ly(mls, x = ~club, y = ~base_salary, color = ~position,
        type = "bar", mode = "stack", 
        marker = list(size = 10, opacity = 0.8)) %>%
  layout(title = "Scatter Plot of Position vs Base Salary", 
         xaxis = list(title = "Club"),
         yaxis = list(title = "Base Salary"))

Example #3

Lastly, we can add a Z axis and create a 3D interactive model that allows individuals to see the data interactively. The type would change to “Scatter3d” and the mode would be “markers”. This will allow us to create an interactive 3D model:

# 3D Scatter Plot
plot_ly(mls, x = ~club, y = ~base_salary, z = ~position,
        color = ~position, type = "scatter3d", mode = "markers", 
        marker = list(size = 8, opacity = 0.8)) %>%
  layout(title = "3D Scatter Plot of Position vs Base Salary on Each MLS Team",
         scene = list(xaxis = list(title = "Club"),
                      yaxis = list(title = "Base Salary"),
                      zaxis = list(title = "position")))