在ggplotly()中设置图例位置和工具提示文本

问题描述 投票:0回答:1

在这个图中,我想在工具提示文本中使用单独的行,但我什么也没看到。另外,我的图例从底部移动到了右侧,这是我不想要的。

Date,Total_Value, Attribute Name and Percentage

	
r ggplotly
1个回答
0
投票

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")

结果截图:

result

© www.soinside.com 2019 - 2024. All rights reserved.