在下面的图中,我希望条形图不要像碎片一样显示。
Total Value
作为整体显示,而不是针对每件作品,并且要显示的年份不是 as(factor) Year
在悬停框中。
counts<-structure(list(Year = c(2023, 2023, 2023, 2023, 2023, 2024, 2024,
2024, 2024, 2024), `Attribute Name` = c("2020", "2021", "2022",
"2023", "2024", "2020", "2021", "2022", "2023", "2024"), `Sub Header 1` = c("Year of first Trodelvy LOT",
"Year of first Trodelvy LOT", "Year of first Trodelvy LOT", "Year of first Trodelvy LOT",
"Year of first Trodelvy LOT", "Year of first Trodelvy LOT", "Year of first Trodelvy LOT",
"Year of first Trodelvy LOT", "Year of first Trodelvy LOT", "Year of first Trodelvy LOT"
), Total_Value = c(1, 11, 17, 9, 0, 3, 33, 51, 27, 1)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(
Year = c(2023, 2023, 2023, 2023, 2023, 2024, 2024, 2024,
2024, 2024), `Attribute Name` = c("2020", "2021", "2022",
"2023", "2024", "2020", "2021", "2022", "2023", "2024"),
.rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L), .drop = TRUE))
library(plotly)
plot <- ggplot(counts, aes(x = as.factor(Year), y = Total_Value, fill = `Sub Header 1`)) +
geom_bar(stat = "identity",width=0.1) +
labs(title = "ADC Treatment Start by Calendar Year",
x = "Year of ADC start",
y = "Patient Counts") +
theme_minimal()
# Customize colors
plot <- plot + scale_fill_manual(values = c("maroon", "darkblue"))
# Place legend under the plot
plot <- plot + theme(legend.position = "bottom")
# Display the plot
ggplotly(plot)
这就是你所追求的吗?请注意,每年的
fill =
变量都是相同的,因此颜色是相同的。我已使用 dplyr::first()
返回“子标题 1”值,因为它们在年份内没有差异。
library(dplyr)
library(plotly)
# Summarise counts data
counts1 <- ungroup(counts) |>
summarise(`Sub Header 1` = first(`Sub Header 1`),
Total_Value = sum(Total_Value), .by = Year)
plot <- ggplot(counts1, aes(x = Year, y = Total_Value, fill = `Sub Header 1`)) +
geom_bar(stat = "identity",width=0.1) +
labs(title = "ADC Treatment Start by Calendar Year",
x = "Year of ADC start",
y = "Patient Counts") +
theme_minimal()
# Customize colors
plot <- plot + scale_fill_manual(values = c("maroon", "darkblue"))
# Place legend under the plot
plot <- plot + theme(legend.position = "bottom")
# Display the plot
ggplotly(plot)