Plotly’s R graphing library makes interactive, publication-quality web graphs. More specifically it gives us the ability to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, and 3D charts.
In this tutorial we are going to make a first step in plotly’s world by learning to create some basic charts enhanced with proper layouts that the plotly package provides.
For other parts of this series follow the tag plotly visualizations
PACKAGE INSTALLATION & DATA
The first thing you have to do is install and load the plotly package with:
install.packages("plotly")
library(plotly)
Moreover we need some data to work with. We will create x, y and y3 with numeric values in order to use them later in our examples.
x =c(1,2,3,4,5 )
y =c(6,7,8,9,10)
y3=c(-6,-7,-8,-9,-10)
NOTE: Because of the fact that all plotly graphics are web graphics,if you’re not using RStudio (which provides a built-in browser), you’ll notice that all plots open in your web browser.
Line Plot
The first thing we have to do is call plot_ly()
. The first argument is the data that we want to plot (x & y), the second describes the type of graph, while the the third sets how our data will be displayed in the graph.
Here we want a line plot to be displayed so we choose “lines”. Look at the example below.
plot_ly (
x =c(1,2,3),
y =c(4,5,6),
type = "scatter" ,
mode = "lines"
)
Scatter Plot
The first thing we have to do is call plot_ly()
. The first argument is the data that we want to plot (x & y), the second describes the type of graph, while the the third sets how our data will be displayed in the graph.
Here we want a scatter plot to be displayed so we choose “markers”, which is the default choise, therefore it can be omitted. Look at the example below.
plot_ly (
x =c(1,2,3),
y =c(4,5,6),
type = "scatter" ,
mode = "markers"
)
Bar Plot
The first thing we have to do is call plot_ly()
. The first argument is the data that we want to plot (x & y), the second describes the type of graph, which now is a bar.
plot_ly (
x =c(1,2,3),
y =c(4,5,6),
type = "bar"
)
Bubble Chart
The first thing we have to do is call plot_ly()
. The first argument is the data that we want to plot (x & y), the second describes the type of graph, which now is a bubble chart, while the the third sets how our data will be displayed in the graph.
Here we want a bubble plot to be displayed so we choose “markers”, which is the default choise, therefore it can be omitted. Then we set the size for every spot and last but not least its color. Look at the example below.
plot_ly (x =c(1, 2, 3 ),
y =c(4,5, 6 ),
type = "scatter" ,
mode = "markers" ,
size =c( 2,6,9 ),
marker = list(color =c( "red","black","yellow" )))
Heatmap
The first thing we have to do is call plot_ly()
. The first argument is the data that we want to plot, which is the “volcano” dataset and will be given as a numerix matrix “z”, the second describes the type of graph, which now is a heatmap. Look at the example below.
plot_ly( z=volcano,
type = "heatmap")
Area Plot
The first thing we have to do is call plot_ly()
. The first argument is the data that we want to plot (x & y), the second describes the type of graph, while the the third sets how our data will be displayed in the graph.
Here we want an area plot to be displayed so we choose “lines” in combination with the fill
argument. Look at the example below.
plot_ly (
x =c(1,2,3),
y =c(4,5,6),
type = "scatter" ,
mode = "lines",
fill = "tozeroy"
)
Layout
Now let’s enhance the layout of our plot. First we will see how to add trace to this. We do this if we want to separate our markers in groups. Look at the example below, in which the first group has y > 0 and the other y < 0:
plot_ly (
x =c(1,2,3 ),
y =c(4,5,6),
y3 =c(-4,-5,-6),
type = "scatter",
mode = "markers"
) %>%
add_trace(x=x,y=y3 )
We can also add the trace as a legend in our plot, set its precise position and color it. Look at the example below:
plot_ly (
x =c(1,2,3 ),
y =c(4,5,6),
y3 =c(-4,-5,-6),
type = "scatter",
mode = "markers"
) %>%
add_trace(x=x,y=y3 )
%>%
layout(legend =
list(
x = 1 ,
y =1 ,
bgcolor = "yellow" ))
The last thing we should be able to do when creating a plot is dealing with the axes. First we create a list which contains all the axes’ features and then we attach this list to the two axes like the example below:
axis_template %
layout(xaxis = axis_template,
yaxis = axis_template )
Leave a Reply