Create a plotly graph of your choosing that represents at least two variables, one of which must be a categorical variable.
This plot can be a scatter plot, overlayed density plots (graphing variable is continuous, separate densities grouped by categorical variable), etc. choropleth maps could also be on the list…you have to admit they look kinda cool.
You can use any data of your choosing, just reference this.
The graph must include:
customized hover text that is informative to the graphing elements in the plot
separate color to represent groups
labeled axes and appropriate title
Include at least a 1-paragraph discussion about the graph. Discuss what is being plotted and what information is being displayed in the graph. Discuss any information that the reader may gain from hovering the cursor over graphing elements. Discuss any issues/chalenges you had (if any) while making the plot, and you you dealt with or overcame them.
diabetes_Data <- read.csv("D://diabetes.csv")
str(diabetes_Data)
## 'data.frame': 768 obs. of 9 variables:
## $ Pregnancies : int 6 1 8 1 0 5 3 10 2 8 ...
## $ Glucose : int 148 85 183 89 137 116 78 115 197 125 ...
## $ BloodPressure : int 72 66 64 66 40 74 50 0 70 96 ...
## $ SkinThickness : int 35 29 0 23 35 0 32 0 45 0 ...
## $ Insulin : int 0 0 0 94 168 0 88 0 543 0 ...
## $ BMI : num 33.6 26.6 23.3 28.1 43.1 25.6 31 35.3 30.5 0 ...
## $ DiabetesPedigreeFunction: num 0.627 0.351 0.672 0.167 2.288 ...
## $ Age : int 50 31 32 21 33 30 26 29 53 54 ...
## $ Outcome : int 1 0 1 0 1 0 1 0 1 1 ...
chooseCRANmirror(ind=1)
install.packages("plotly")
## Installing package into 'C:/Users/ajayv/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'plotly' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\ajayv\AppData\Local\Temp\Rtmp2nstrv\downloaded_packages
# Load required libraries
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
# Create a scatter plot with Glucose on the x-axis, BMI on the y-axis, and Outcome as the categorical variable
plot <- plot_ly(data = diabetes_Data, x = ~Glucose, y = ~BMI, color = ~factor(Outcome),
type = 'scatter', mode = 'markers', marker = list(size = 10)) %>%
layout(title = "Diabetes Dataset: Glucose vs. BMI",
xaxis = list(title = "Glucose"),
yaxis = list(title = "BMI"),
hovermode = "closest",
hoverlabel = list(bgcolor = "white", font = list(family = "Arial", size = 12),
align = "auto"))
# Display the plot
plot
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
The plot is a scatter plot visualizing the relationship between Glucose levels and BMI (Body Mass Index) in individuals from the diabetes dataset. Each point on the plot represents a person, with their Glucose level on the x-axis and BMI on the y-axis. The points are color-coded based on whether the individual is diabetic or not, with diabetic individuals marked in one color and non-diabetic individuals in another color.
Hovering over a point on the graph displays customized hover text that provides informative details about that specific individual, including their Glucose level, BMI, and diabetic status. This interactive feature allows the reader to gain insights into the characteristics of each data point directly from the graph.
By incorporating interactive elements like hover text and color-coded groups, the plot enhances the reader’s understanding of the dataset by visually representing the relationships between Glucose levels, BMI, and diabetes status.
Create an animated plotly graph with a data set of your choosing. This can be, but does not have to be a scatter plot. Also, the animation does not have to take place over time. As mentioned in the notes, the frame can be set to a categorical variable. However, the categories the frames cycle through should be organized (if needs be) such that the progression through them shows some pattern.
This graph should include:
Aside from the graphing variable, a separate categorical variable. For example, in our animated scatter plot we color grouped the points by continent.
Appropriate axis labels and a title
Augment the frame label to make it more visible. This can include changing the font size and color to make it stand out more, and/or moving the frame label to a new location in the plotting region. Note, if you do this, make sure it is till clearly visible and does not obstruct the view of your plot.
Include at least a 1-paragraph discussion about the plot. Discuss what you are plotting and what trends can be seen throughout the animation. Discuss any issues, if any, you ran into in making the plot and how you overcame them.
# Load required libraries
library(plotly)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Calculate the average BMI for diabetic and non-diabetic individuals
average_bmi <- diabetes_Data %>%
group_by(Outcome) %>%
summarise(Avg_BMI = mean(BMI, na.rm = TRUE))
# Create frames for animation
frames <- list(
list(name = "Diabetic", data = list(x = ~Outcome, y = ~Avg_BMI, type = "bar",
frame = ~Outcome, color = ~Outcome, colors = c("#1f77b4"))),
list(name = "Non-Diabetic", data = list(x = ~Outcome, y = ~Avg_BMI, type = "bar",
frame = ~Outcome, color = ~Outcome, colors = c("#ff7f0e")))
)
# Create an animated bar chart
plot <- plot_ly(data = average_bmi, x = ~Outcome, y = ~Avg_BMI, type = 'bar',
frame = ~Outcome, color = ~Outcome, colors = c("#1f77b4", "#ff7f0e"),
frames = frames) %>%
layout(title = "Average BMI by Diabetic Status",
xaxis = list(title = "Diabetic Status"),
yaxis = list(title = "Average BMI"),
hovermode = "closest",
hoverlabel = list(bgcolor = "white", font = list(family = "Arial", size = 12),
align = "auto"))
# Display the plot
plot
## Warning: 'bar' objects don't have these attributes: 'frames'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
The animated bar chart visualizes the average BMI (Body Mass Index) of individuals grouped by their diabetic status (Diabetic or Non-Diabetic) from the diabetes_Data dataset. Each bar represents the average BMI for the corresponding diabetic status category, with colors distinguishing between Diabetic (blue) and Non-Diabetic (orange) groups.
Throughout the animation, we can observe trends in the average BMI between diabetic and non-diabetic individuals. Typically, diabetic individuals may have a higher average BMI compared to non-diabetic individuals, reflecting the association between diabetes and obesity or higher body mass.
One challenge encountered while creating the plot was configuring the animation slider. The initial error related to selecting elements in the slider index was resolved by manually specifying frames within the plot_ly function instead of relying on the slider for frame selection. This approach provided more control over the animation frames and eliminated the error, resulting in a clear and informative animated bar chart showcasing the average BMI trends based on diabetic status.