Plotly aaron s 2024-01-16 Introduction This web page presents an interactive data visualization created using Plotly in R. Suppose we’re analyzing a dataset that tracks the daily average temperatures and humidity levels in a city over a year. Our objective is to visualize how these two variables change over time and observe any patterns or correlations between temperature and humidity.

Plot Design For this scenario, we can create a Plotly plot with the following features:

X-axis: Days of the year (1-365). Y-axis: Will have two scales; one for temperature (in °C) and one for humidity (in %). Lines: Two lines on the plot; one for daily average temperature and another for humidity level. Interactivity: Users can hover over points on the plot to see the exact temperature and humidity values for each day. R Code for the Plot: library(plotly)

Loading required package: ggplot2

Attaching package: ‘plotly’

The following object is masked from ‘package:ggplot2’:

last_plot

The following object is masked from ‘package:stats’:

filter

The following object is masked from ‘package:graphics’:

layout

Generate sample data

set.seed(123) # for reproducibility days <- 1:365 temperature <- sin(seq(0, 2 * pi, length.out = 365)) * 10 + 20 # Simulated temperature humidity <- runif(365, min = 40, max = 90) # Simulated humidity

data <- data.frame(days, temperature, humidity)

Create the plot

fig <- plot_ly(data, x = ~days)

Add temperature trace

fig <- fig %>% add_trace(y = ~temperature, name = ‘Temperature’, mode = ‘lines’, type = ‘scatter’, yaxis = ‘y1’)

Add humidity trace

fig <- fig %>% add_trace(y = ~humidity, name = ‘Humidity’, mode = ‘lines’, type = ‘scatter’, yaxis = ‘y2’)

Layout adjustments

fig <- fig %>% layout( title = ‘Daily Temperature and Humidity Levels’, xaxis = list(title = ‘Day of the Year’), yaxis = list(title = ‘Temperature (°C)’), yaxis2 = list(title = ‘Humidity (%)’, overlaying = ‘y’, side = ‘right’) )

```

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.