ha$had_heart_attack <- factor(ha$had_heart_attack, levels = c("Yes", "No"))
# Aggregate data to count occurrences by state, sex, race_ethnicity_category, and had_heart_attack
agg_data <- ha %>%
group_by(state, sex, race_ethnicity_category, had_heart_attack) %>%
summarise(count = n(), .groups = 'drop')
# Create the Plotly bar graph
p <- agg_data %>%
plot_ly(x = ~state, y = ~count, color = ~had_heart_attack, type = 'bar',
hoverinfo = 'text',
text = ~paste('State: ', state, '<br>Heart Attack: ', had_heart_attack, '<br>Count: ', count)) %>%
layout(title = "Number of Heart Attacks per State by Sex and Ethnicity",
xaxis = list(title = "State"),
yaxis = list(title = "Number of Heart Attacks"),
barmode = 'dodge', # Bars side-by-side for Yes and No
facet = list(rows = 1, columns = 2,
categoryorder = 'total ascending')) # Facet by sex and race_ethnicity_category
# Show plot
p
## 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
## Warning: 'layout' objects don't have these attributes: 'facet'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
# Loading necessary libraries
library(dplyr)
library(plotly)
# Assuming gf is the dataset and has the required columns
ha <- gf %>%
select(sex, had_heart_attack) %>%
group_by(sex, had_heart_attack) %>%
summarise(count = n()) %>%
ungroup()
## `summarise()` has grouped output by 'sex'. You can override using the `.groups`
## argument.
# Create an interactive plot with plotly
interactive_plot <- plot_ly(ha,
x = ~sex,
y = ~count,
color = ~had_heart_attack,
type = 'bar',
barmode = 'dodge',
text = ~paste('Count:', count),
hoverinfo = 'text') %>%
layout(title = 'Count of Males and Females with Heart Attack Status',
xaxis = list(title = 'Sex'),
yaxis = list(title = 'Count'),
barmode = 'dodge')
# Display the plot
interactive_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
## Warning: 'bar' objects don't have these attributes: 'barmode'
## 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'
## Warning: 'bar' objects don't have these attributes: 'barmode'
## 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'
ha$had_heart_attack <- factor(ha$had_heart_attack, levels = c("Yes", "No"))
# Aggregate data to count occurrences by state, sex, race_ethnicity_category, and had_heart_attack
agg_data <- gf %>%
group_by(state, sex, race_ethnicity_category, had_heart_attack) %>%
summarise(count = n(), .groups = 'drop')
# Create the Plotly bar graph
p <- agg_data %>%
plot_ly(x = ~state, y = ~count, color = ~race_ethnicity_category, type = 'bar',
hoverinfo = 'text',
text = ~paste('State: ', state, '<br>Heart Attack: ', had_heart_attack, '<br>Count: ', count)) %>%
layout(title = "Number of Heart Attacks per State by Sex and Ethnicity",
xaxis = list(title = "State"),
yaxis = list(title = "Number of Heart Attacks"),
barmode = 'dodge', # Bars side-by-side for Yes and No
facet = list(rows = 1, columns = 2,
categoryorder = 'total ascending')) # Facet by sex and race_ethnicity_category
# Show plot
p
## Warning: 'layout' objects don't have these attributes: 'facet'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
ha$had_heart_attack <- factor(ha$had_heart_attack, levels = c("Yes", "No"))
# Aggregate data to count occurrences by state, sex, race_ethnicity_category, and had_heart_attack
agg_data <- gf %>%
group_by(state, sex, race_ethnicity_category, had_heart_attack) %>%
summarise(count = n(), .groups = 'drop')
# Create the Plotly bar graph
p <- agg_data %>%
plot_ly(x = ~state, y = ~count, color = ~race_ethnicity_category,
type = 'bar',
hoverinfo = 'text',
text = ~paste('State: ', state, '<br>Heart Attack: ', had_heart_attack, '<br>Count: ', count),
split = ~had_heart_attack) %>% # Split by 'had_heart_attack' to separate bars for Yes/No
layout(title = "Number of Heart Attacks per State by Sex and Ethnicity",
xaxis = list(title = "State"),
yaxis = list(title = "Number of Heart Attacks"),
barmode = 'dodge', # Bars side-by-side for Yes and No
facet = list(rows = 1, columns = 2,
categoryorder = 'total ascending'),
legend = list(title = list(text = "Heart Attack Status"),
x = 1, y = 1)) # Position and title for the legend
# Show plot
p
## Warning: 'layout' objects don't have these attributes: 'facet'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'