Plotly is a library that makes quality, interactive graphs. It is the superior plotting package as all graphs are created to be interactive. Other packages require coders to take extra steps to make any graphs interactive. Plotly graphs can be created in Python, Julia, Jupyter, or R, as it is used here. In this code through, we will discuss creating Axes, labeling the lines on a graph, adding ticks, legends, and hover.
In order to create a 2D graph, the x and y axes need to be labeled. ‘x’ list all the numbers between 1 and 11. ‘y_m’ and ‘y_f’ lists the summation of awards conferred to males and females, respectively, in 2018-2019 at Title IV institutions.
= c(1:11)
x
= c(257238, 162453, 407220, 14921, 857545, 17612, 326186, 6235, 36286, 48727, 756)
y_m
= c(288781, 266877, 629456, 9726, 1155309, 30947, 507520, 15992, 37308, 63157, 1334) y_f
The first line of code assigns ‘fig’ to be a Plotly graph. It specifies that the data should be placed in a scatter plot with the points connected via lines and ‘x’ data should be placed on the x-axis and ‘y_m’ data should be placed on the y-axis. The line assigns ‘y_f’ to the y-axis as well. The third line calls the variable ‘fig’ in order to display the graph.
<- plot_ly(type = "scatter", mode = "lines", x = ~x, y = ~y_m )
fig
<- fig %>% add_trace(y = ~y_f)
fig
fig
The labels of the lines can be assigned by adding ‘name = “Title”’ to the function that plots that line. The color of the line can also be adjusted by adding ‘line = list(color = ’rgb(###, ###, ###)’) to the function that plots the line.
<- plot_ly(type = "scatter", mode = "lines", x = ~x, y = ~y_m, name = "All Men", line = list(color = 'rgb(255, 198, 39)') )
fig
<- fig %>% add_trace(y = ~y_f, name = "All Women", line = list(color = 'rgb(140, 29, 64)' ) )
fig
fig
The type of font, the size, and the title of the axes and the legend can all be edited using the list function. A legend is automatically created with more than one line added for the y-axis. The legend allows users to choose which lines that appear on the graph by selecting and unselecting the labels in the legend. The title of the graph, the axes, and the legend specifications call be assigning them to the figure by using the layout function.
<- list(family = "Arial, sans serif" ,
a_x size = 14,
title = "Degrees")
<- list(family = "Arial, sans serif" ,
a_y size = 14,
title = "Number of Degrees")
<- list(family = "Arial, sans serif" ,
l size = 12)
<- fig %>% layout(title = "Total Number of awards conferred by Title IV institutions in 2018-19",
fig xaxis = a_x ,
yaxis = a_y,
legend = l)
fig
To ensure that each the title of each tick is being accurately labeled, ‘ticktext’ and ‘tickvals’ can be used to assign them in the correct order.
<- list(ticktext = list("Less-than-1-year", "At-least-1- but less-than-2-years", "Associate’s degrees", "At-least-2- but less-than-4-years", "Bachelor’s degrees", "Post-baccalaureate certificates", "Master’s degrees", "Post-master’s certificates", "Doctor’s degrees— research/scholarship", "Doctor’s degrees—professional practice", "Doctor’s degrees—other"),
a_x tickvals = list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11))
<- fig %>% layout(xaxis = a_x)
fig fig
Currently, the values of each data point can be found be hovering over each point respectively. However, some data points overalapp, which can make it difficult to access. To solve this, ‘hovermode’ can be used to list each of the data points per tick on the x-axis. The Text shows the degree conferred, the total number of men and the total number women.
<- fig %>% layout(hovermode = "x unified")
fig
fig
Plotly can be used to create several different types of graphs including 3D graphs. To learn about the data used in this code through and more ways to use plotly, visit the links in the ‘Further Resources’ section.
Learn more about [plotly, technique, dataset] with the following:
About Plotly Hyperlink Text
Plotly in R Hyperlink Text
Dataset Hyperlink Text
This code through references and cites the following sources:
Plotly (2021). Source I. Hyperlink Text
US Department of Education, National Center for Education Statistics (2019). Source II. Hyperlink Text