Plotly is part of the “html widgets” family in R. It is a library of functions in R that allows you to translate ggplot2 graphics to interactive web visualizations. It llows you to make beautiful, interactive, exportable figures in just a few lines of code.
For further information, you can allows check some online resources such as here.
Some of you might confuse ggplot2 with plotly and the difference is simple. Although ggplot2 package is your first choice when it comes to data visualisations, however, it has some limitations to when it comes to user ineractvitity. Such problems arise while creating interactive documents using R Markdown or dashboard apps in R Shiny.
The static ggplot2 chart can quickly be made interactive by simply placing it inside the ggplotly function (after installing and loading plotly).
The below static plot has now been brought to life. Users can now zoom, hover, pan and export the plot, and more.
You can install “plotly” with the function ‘install.packages()’ and load it with ‘library ()’.
The below line graphs demonstrate how the average bill is different for females and males during lunch and dinner. However, the first line graph is illustrated using ggplot and the second one is illustrated using ggplotly.
dat1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"),
levels = c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
figure1 <- ggplot(data = dat1,
aes(x = time,
y = total_bill,
group = sex,
shape = sex,
colour = sex)) +
geom_line(aes(linetype = sex),
size = 1) + # Set linetype by sex
geom_point(size = 5) + # Use larger points, fill with white
scale_colour_hue(name ="Sex", # Set legend title
l = 30) + # Use darker colors (lightness=30)
scale_shape_manual(name ="Sex",
values = c(22,21)) + # Use points with a fill color
scale_linetype_discrete(name ="Sex") +
xlab("Time of day") + ylab("Total bill") + # Set axis labels
ggtitle("Average bill for 2 people") + # Set title
theme_bw()
figure1dat1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"),
levels =c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
p <- ggplot(data = dat1, aes(x =time,
y = total_bill,
group = sex,
shape = sex,
colour = sex)) +
geom_line(aes(linetype = sex),
size = 1) + # Set linetype by sex
geom_point(size = 5) + # Use larger points, fill with white
scale_colour_hue(name ="Sex", # Set legend title
l =30) + # Use darker colors (lightness=30)
scale_shape_manual(name="Sex",
values=c(22,21)) + # Use points with a fill color
scale_linetype_discrete(name ="Sex") +
xlab("Time of day") +
ylab("Total bill") + # Set axis labels
ggtitle("Average bill for 2 people") + # Set title
theme_bw()
figure2 <- ggplotly(p)
figure2** plot_ly : Initiate a plotly visualization
as_widget : Convert a list to a plotly htmlwidget object
gg2list: Convert a ggplot to a list.
add_data : Add data to a plotly visualization
plotly-shiny :Shiny bindings for plotly
geom_line : Connect observations, ordered by x value.
plotly_data : Obtain data associated with a plotly graph
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Dash apps are rendered in the web browser. You can deploy your apps to servers and then share them through URLs. Since Dash apps are viewed in the web browser, Dash is inherently cross-platform and mobile ready.
Community member Jamie Nuñez introduced a new colorscale to plotly.js, which is called “Cividis”. It was developed by her and her colleagues. Their purpose was “Optimizing colormaps with consideration for color vision deficiency to enable accurate interpretation of scientific data.” As a side note, plotly.js is the open source javascript graphing library that powers plotly.
Please check the below URLs if you feel a bit curious to know more about plotly and what has been discussed above.
Sample datasets
Getting Started
Functions
Start with ggplot2