About Plotly

Plotly is a powerful interactive visualization library that allows users to create web-based, dynamic charts right from R. It offers a wide variety of graph types, such as scatter plots, bar charts, line graphs, 3D plots, maps, and more. One of Plotly’s biggest advantages is the interactivity it brings to R visualizations, including zooming, panning, and tooltips that display information when hovering over data points.

Why we use Plotly in R

Interactivity: Unlike static plots, Plotly’s graphs are highly interactive. Web-Integration: Outputs can be embedded in web apps, dashboards, or HTML reports. Customization: Full control over elements like axes, colors, legends, and layouts. 3D Capabilities: Support for 3D scatter plots, surface plots, and more. Integration: Works seamlessly with R Markdown, Shiny apps, and even ggplot2 through ggplotly().

Core Functions in Plotly

  1. plot_ly() - The core function to create a plot from scratch.
  2. layout() - Used to customize the layout, including titles,axes, and legends.
  3. ggplotly() - Converts a ggplot2 graph to an interactive Plotly graph.
  4. subplot() _ Combines multiple plots into a grid layout.

Creating Basic Visualizations with Plotly

Scatter Plot

A scatter plot is used to visualize relationship between two variables.

library(plotly)
fig = plot_ly(
  x = ~c(1, 2, 3, 4, 5), 
  y = ~c(10, 11, 12, 13, 14), 
  type = 'scatter',    
  mode = 'markers'
)
fig
# Bar Plot
r library(plotly) fig = plot_ly( x = ~c("A", "B", "C"), y = ~c(10, 20, 15), type = 'bar' ) %>% layout(title = "Bar Plot Example")

Air Quality dataset

The Air Quality dataset in R is a built-in dataset that contains daily air quality measurements in New York from May to September 1973. It includes variables such as ozone levels, solar radiation, wind speed, temperature, and more. The dataset provides an excellent opportunity to explore and visualize air quality data.

The Air Quality dataset provides a wealth of information that can be visualized to explore patterns and relationships in air quality data. The examples below demonstrate how to create different types of interactive plots using Plotly in R, enhancing the analysis of this dataset.

Scatter Plot of Ozone vs. Temperature

This plot shows the relationship between ozone levels and temperature.

library(plotly)

plot_ly(data = airquality,
        x = ~Temp,
        y = ~Ozone,
        type = 'scatter',
        mode = 'markers',
        marker = list(size = 10,color = 'purple', opacity = 0.5))%>%
  layout(title = "Ozone Levels vs Temperature",
         xaxis = list(title = "Temperature(°F)"),
         yaxis = list(title = "Ozone (ppb)"))
# Box Plot of Ozone by Month
This box plot helps visualize the distribution of ozone levels across different months.
r plot_ly(data = airquality, x = ~factor(Month), y = ~Ozone, type = 'box') %>% layout(title = "Ozone Levels by Month", xaxis = list(title = "Month"), yaxis = list(title = "Ozone (ppb)"))
{=html} <div class="plotly html-widget html-fill-item" id="htmlwidget-e852e048e9a574131e2e" style="width:672px;height:480px;"></div> <script type="application/json" data-for="htmlwidget-e852e048e9a574131e2e">{"x":{"visdat":{"31041136519f":["function () ","plotlyVisDat"]},"cur_data":"31041136519f","attrs":{"31041136519f":{"x":{},"y":{},"alpha_stroke":1,"sizes":[10,100],"spans":[1,20],"type":"box"}},"layout":{"margin":{"b":40,"l":60,"t":25,"r":10},"title":"Ozone Levels by Month","xaxis":{"domain":[0,1],"automargin":true,"title":"Month","type":"category","categoryorder":"array","categoryarray":["5","6","7","8","9"]},"yaxis":{"domain":[0,1],"automargin":true,"title":"Ozone (ppb)"},"hovermode":"closest","showlegend":false},"source":"A","config":{"modeBarButtonsToAdd":["hoverclosest","hovercompare"],"showSendToCloud":false},"data":[{"fillcolor":"rgba(31,119,180,0.5)","x":["5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","6","6","6","6","6","6","6","6","6","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","7","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9"],"y":[41,36,12,18,28,23,19,8,7,16,11,14,18,14,34,6,30,11,1,11,4,32,23,45,115,37,29,71,39,23,21,37,20,12,13,135,49,32,64,40,77,97,97,85,10,27,7,48,35,61,79,63,16,80,108,20,52,82,50,64,59,39,9,16,78,35,66,122,89,110,44,28,65,22,59,23,31,44,21,9,45,168,73,76,118,84,85,96,78,73,91,47,32,20,23,21,24,44,21,28,9,13,46,18,13,24,16,13,23,36,7,14,30,14,18,20],"type":"box","marker":{"color":"rgba(31,119,180,1)","line":{"color":"rgba(31,119,180,1)"}},"line":{"color":"rgba(31,119,180,1)"},"xaxis":"x","yaxis":"y","frame":null}],"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.20000000000000001,"selected":{"opacity":1},"debounce":0},"shinyEvents":["plotly_hover","plotly_click","plotly_selected","plotly_relayout","plotly_brushed","plotly_brushing","plotly_clickannotation","plotly_doubleclick","plotly_deselect","plotly_afterplot","plotly_sunburstclick"],"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>

Line Plot of Ozone Over Time

This line plot shows ozone levels change over time.

airquality$Date = as.Date(paste(1973, airquality$Month, airquality$Day, sep = "-"))
plot_ly(data = airquality, 
                x = ~Date, 
                y = ~Ozone, 
                type = 'scatter', 
                mode = 'lines+markers', 
                line = list(color = 'red')) %>%
  layout(title = "Ozone Levels Over Time",
         xaxis = list(title = "Date"),
         yaxis = list(title = "Ozone (ppb)"))