Jerad Acosta
1/13/2017
Plotly and more specifically for our purposes the Plotly R API provides an inviting collection of visualization and interaction features for the data explorer and visualization producer.
Plotly’s R API is an amazing resource. It’s helped me outgrow the ggplot2 / tidyverse / Hadley reliant developer whenever it came to data products I knew others would have to rely on after I was gone.
This will cover a simple annotation feature I was unable to easily find documentation that was immediate and concise: the 9 arrow head styles available in the annotations attributes listed on Plotly’s R API reference guide
For all of Plotly’s API docs and libraries for Python, Matlap and Js as well as R check out here
to skip to a visual of the 9 arrowhead options go straight to final slight
Understandably, there is only so much you can fit into a useful resource that is also as easy to navigate as this one.
One such case is the arrowhead which is part of the annotations attributes in the Plotly call signature Layout.
Which can also be quite confusing until you’ve stuggled with it for a little then gone back and read this line conveniently placed atop the R figure reference guide
Plotly’s graph description places attributes into two categories: traces (which describe a single series of data in a graph) and layout attributes that apply to the rest of the chart, like the title, xaxis, or annotations).
Plotly’s R reference on arrowhead setting says:
arrowhead (
integer between or equal to 0 and 8)
default:1
Sets the annotation arrow head style.
For our arrowhead options we will be using a slightly modified version of an example for Plotly’s site in hopes that it will excite and inspire others to try this library out for themselves.
# testing scripts for Plotly Arrowhead Rpubs
library(plotly)
## This first list is where the magic is happening
# our annotations list identifies the data we wish to point out
# We will point out the car with the best mpg in the familiar mtcars set
a <- list(
# we give the x value by finding the index of the best mpg car
# then using the weight variable since that is our x-axis
x = mtcars[which(mtcars$mpg %in% max(mtcars$mpg)),'wt'],
# Similarly we enter our y value into the list
y = mtcars[which(mtcars$mpg %in% max(mtcars$mpg)),'mpg'],
# for the text of our annotation we will ask the rowname for the car
text = rownames(mtcars[which(mtcars$mpg %in% max(mtcars$mpg)),]),
# we use xref & yref to say that x & y are actual coordinates
# you can do other stuff with annotations like use pixels or references
# to write a message or leave a shape on your graph
xref = "x",
yref = "y",
# this is to inform say that we would like to use an arrow
# which will be directed toward the position defined by x & y
showarrow = TRUE,
# finally, our arrowhead option: select a integer 0-8 inclusive
# Let's start from the beginning
arrowhead = 0,
# because axref & ayref have not been set their default values are 'pixel'
# thus, for ax a positive number will take the arrow from right to left
ax = 40,
# for ay a negative number will take the arrow from top to bottom
ay = -40
)
# create our plotly object
p <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
add_markers() %>%
# add our custom annotation list to the annotations setting in the layout
layout(annotations = a)
# and let's see our graph and arrow
p## Warning in arrange_impl(.data, dots): '.Random.seed' is not an integer
## vector but of type 'NULL', so ignored
Option arrowhead = 0 is predictably simple. The Scatter plot or original shape of the data point is maintained and a simple line connects the annotation to the data point.
Rather than run through 8 more iterations of the above, we will assume the previous example presented enough information to create and define and arrowhead annotation.
A list arrows() has been creating which contains 9 elements, each a list following the same or similar settings to what was used in the last graph.
Note the arrows have been colored red with the setting arrowcolor = 'red' for a more clear example of the 9 possible arrowheads
p.all <- plot_ly(data = mtcars, x = ~wt, y = ~mpg) %>%
add_markers() %>%
layout(annotations = arrows)
p.allPlotly and in particular to this presentation’s use, Plotly’s R graphing library API is an excellent resource for generating interacting plots that are highly customizable.
Far more than custom annotation’s and arrows, Plotly is capable of:
> - ggplot2 intergration and extension
> - 3D interactive JavaScript plots
> - htmlwidget interfacing
> - animations
> - and a host of custom JavaScript behaviors
For more: check out CRAN’s Package ‘plotly’ PDF for a great introduction to the package and its methods.
For those eager enough to start learning before my next tutorial Check out the Plotly Book written by Carson Sievert, Plotly’s R package primary contributor.