Plotly is a popular data visualization library that provides powerful interactive charts for data analysis. It is widely used for creating dashboards, web-based visualizations, and interactive plots. Plotly allows you to create a wide variety of plots, including scatter plots, line charts, bar charts, boxplots, 3D plots, and more.
The ChickWeight dataset in R is a popular dataset that tracks the growth of chicks over time. The dataset records the weight of chicks under different diet treatments at multiple time points. By using Plotly, you can create interactive visualizations to explore how the chicks’ weight changes over time under different diet conditions.
#Load libraries
library(ggplot2)
library(plotly)
##
## 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
#Load chickweight data
data("ChickWeight")
head(ChickWeight)
## weight Time Chick Diet
## 1 42 0 1 1
## 2 51 2 1 1
## 3 59 4 1 1
## 4 64 6 1 1
## 5 76 8 1 1
## 6 93 10 1 1
#convert to dataframe
ChickWeight=as.data.frame(ChickWeight)
#Boxplot of Weight by Diet
plot_ly(ChickWeight, y = ~weight, color = ~factor(Diet), type = 'box') %>%
layout(title = "Weight Distribution by Diet",
yaxis = list(title = "Weight (grams)",
zeroline = FALSE),
xaxis = list(title = "Diet"))
#scatterplot of Weight vs Time coloured by Diet
plot_ly(ChickWeight, x = ~Time, y = ~weight, color = ~factor(Diet),
type = 'scatter', mode = 'markers',
text = ~paste("Chick:", Chick)) %>%
layout(title = "Chick Weight vs. Time by Diet",
xaxis = list(title = "Time (days)"),
yaxis = list(title = "Weight (grams)"))
# 3D scatter plot with weight, time, and chick as axes
plot_ly(ChickWeight, x = ~Time, y = ~weight, z = ~Chick, color = ~factor(Diet),
type = 'scatter3d', mode = 'markers') %>%
layout(title = "3D Plot of Chick Weight Over Time by Chick and Diet",
scene = list(xaxis = list(title = "Time (days)"),
yaxis = list(title = "Weight (grams)"),
zaxis = list(title = "Chick ID")))
#Animation of Growth Over Time
plot_ly(ChickWeight, x = ~Time, y = ~weight, color = ~factor(Diet),
frame = ~Time, ids = ~Chick,
type = 'scatter', mode = 'lines+markers') %>%
layout(title = "Chick Weight Growth Over Time",
xaxis = list(title = "Time (days)"),
yaxis = list(title = "Weight (grams)"))
Plotly provides a wide variety of ways to visualize the ChickWeight dataset interactively. You can explore how chick growth varies over time with different diets, identify trends, and compare growth rates among diet groups. Whether you’re using scatter plots, box plots, animations, or 3D plots, Plotly allows you to present this data in an engaging and dynamic way.