Plotting graphs with Plotly for R
Recently been using Plotly for R to generate graphs and plots for my dissertation. Its really powerful, open source, and has a nice extension for Visual Studio Code - R Tools.
If you want to use it there is a lot of proper tutorials out there;
- R Tutorials: breaks down all the basics.
- Plotly’s Documentation: examples of all the graphing types.
- R Google Style Guide: quick and dirty style guide, if worried about some common standards.
Plotly - Example
Using a .csv
generated from a different application, plotly makes it very easy to write an R script to visualise the data. An example of one script is below.
install.packages("plotly") # make sure we install the plotly package first
library(plotly) # setup the library
data <- read.csv("../path-to-our-csv/data.csv") # load in our data
p <-
plot_ly(
data,
x = ~t, # match to the t column in data
y = ~x, # match to the x column in data
name = "wx",
type = 'scatter',
mode = 'lines'
) %>%
add_trace(y = ~y, name = "wy", mode = 'lines') %>%
add_trace(y = ~z, name = "wz", mode = 'lines') %>%
layout(title = "Angular Speed Over Time",
yaxis = list(title = "wx, wy, wz")) # make a pretty scatter plot
p # run it
Generates the following plot from this this data.
Here is a few other examples.
Notes
I could have exported to the
Json Scema but it merges the presentation into the data which I didn’t like, so to
keep it simple I just output to .csv
then use different .R
files to visualize the data.