In this plot I want to have in my tooltip text Date,Total_Value, Attribute Name and Percentage
in separate lines and I see nothing. Also my legend moved from bottom to the right which i do not want.
data_counts<-structure(list(Date = c("December - 2023", "December - 2023",
"December - 2023", "February - 2024", "February - 2024", "February - 2024",
"January - 2024", "January - 2024", "January - 2024", "March - 2024",
"March - 2024", "March - 2024"), `Attribute Name` = c("patients_have_both",
"patients_have_ev_without_sg", "patients_have_sg_without_ev",
"patients_have_both", "patients_have_ev_without_sg", "patients_have_sg_without_ev",
"patients_have_both", "patients_have_ev_without_sg", "patients_have_sg_without_ev",
"patients_have_both", "patients_have_ev_without_sg", "patients_have_sg_without_ev"
), Total_Value = c(152, 28, 821, 163, 27, 899, 157, 27, 854,
169, 27, 953), percentage = c(0.151848151848152, 0.027972027972028,
0.82017982017982, 0.149678604224059, 0.0247933884297521, 0.825528007346189,
0.151252408477842, 0.0260115606936416, 0.822736030828516, 0.147084421235857,
0.0234986945169713, 0.829416884247171)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), groups = structure(list(
Date = c("December - 2023", "February - 2024", "January - 2024",
"March - 2024"), .rows = structure(list(1:3, 4:6, 7:9, 10:12), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), .drop = TRUE))
p<-ggplot(data_counts, aes(x = Date, y = Total_Value, fill = `Attribute Name`),
text = paste0(Total_Value, " (", scales::percent(percentage), ")")
) +
geom_bar(stat = "identity",width = 0.3) +
labs(x = "Month of Flatiron Data Cutoff", y = "Patient Counts", title = "ADC Uptake over time among advanced UC") +
scale_fill_manual(values = c("lightblue", "darkblue", "maroon")) +
theme_minimal() +
theme(
legend.position = "bottom" # Position the legend at the bottom
)
ggplotly(p,tooltip = "text")
You need to set the legend position inside the ggplotly()
function. Like this:
library(plotly)
p <- ggplot(data_counts,
aes(x = Date, y = Total_Value, fill = `Attribute Name`,
text = paste("Date:", Date,
"\nTotal Value:", Total_Value, " (", scales::percent(percentage), ")",
"\nName:", `Attribute Name`))) +
geom_bar(stat = "identity", width = 0.3) +
labs(x = "Month of Flatiron Data Cutoff",
y = "Patient Counts",
title = "ADC Uptake over time among advanced UC") +
scale_fill_manual(values = c("lightblue", "darkblue", "maroon")) +
theme_minimal()
ggplotly(p, tooltip = "text") %>%
layout(legend = list(x = 0,
y = -0.2,
xanchor = "left",
yanchor = "bottom",
orientation = "h"))