I am trying to create an interactive graph where you can toggle between 4 different bar charts. I have this code that successfully makes 1 bar chart with a button with 4 options, however it is only showing parts of the first plot on each toggle page, not the different plots. Any ideas on how to fix this?
barp <- ggplot(data = dat, aes(x = AnnualIncome_Label, fill = interest_Label)) +
geom_bar(position = "fill") +
xlab("Annual Income") +
ylab("Proportion") +
ggtitle("Proportional Interest in Clinical Trials by Income") +
labs(fill = "Clinical Trial Interest Level") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_fill_manual(values = c("khaki2", "lemonchiffon1", "#DADAEB", "#9E9AC8", "#6A51A3"))
# Stacked bar chart for "Education" and "Interest_Label"
plot2 <- ggplot(data = dat, aes(x = Ed_Label, fill = interest_Label)) +
geom_bar(position = "fill") +
xlab("Education Level") +
ylab("Proportion") +
ggtitle("Proportional Interest in Clinical Trials by Education Level") +
labs(fill = "Clinical Trial Interest Level") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_fill_manual(values = c("khaki2", "lemonchiffon1", "#DADAEB", "#9E9AC8", "#6A51A3"))
# Stacked bar chart for "EnglishProf_Label" and "Interest_Label"
plot3 <- ggplot(data = dat, aes(x = EnglishProf_Label, fill = interest_Label)) +
geom_bar(position = "fill") +
xlab("English Proficiency Label") +
ylab("Proportion") +
ggtitle("Proportional Interest in Clinical Trials by English Proficiency") +
labs(fill = "Clinical Trial Interest Level") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_fill_manual(values = c("khaki2", "lemonchiffon1", "#DADAEB", "#9E9AC8", "#6A51A3"))
# Stacked bar chart for "HelpMedLit_Label" and "Interest_Label"
plot4 <- ggplot(data = dat, aes(x = HelpMedLit_Label, fill = interest_Label)) +
geom_bar(position = "fill") +
xlab("Help with Medical Literacy Label") +
ylab("Proportion") +
ggtitle("Proportional Interest in Clinical Trials by Help Understanding Medical Literature") +
labs(fill = "Clinical Trial Interest Level") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none") +
guides(fill = FALSE) +
scale_fill_manual(values = c("khaki2", "lemonchiffon1", "#DADAEB", "#9E9AC8", "#6A51A3"))
plotly_plot1 <- ggplotly(plot1)
plotly_plot2 <- ggplotly(plot2)
plotly_plot3 <- ggplotly(plot3)
plotly_plot4 <- ggplotly(plot4)
plotly_plots <- list(plotly_plot1, plotly_plot2, plotly_plot3, plotly_plot4)
# Create buttons for dropdown menu
buttons <- list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE)),
label = "Plot 1"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE, FALSE)),
label = "Plot 2"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE)),
label = "Plot 3"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, TRUE)),
label = "Plot 4")
)
initial_fig <- plotly_plots[[1]]
initial_fig <- initial_fig %>%layout(
title = "Dropdown Menu - Plot Selection",
updatemenus = list(
list(
buttons = buttons
)
)
)
initial_fig
我希望 but子或 drop平的菜菜单在地块——“开发”和地块(开发1,块地——2,块地——开发3,块地——开发4)之间改变。
现在是,它看着它刚刚四分五裂地——1号——四号。