Another option is ggalign, you can
# randomly generated phenograph data
set.seed(1)
TotalPercentage <- data.frame(
`Participant ID` = c("123", "456", "789"),
`1` = 125 * runif(72),
`2` = 75 * runif(72),
`3` = 175 * runif(72),
`4` = 10 * runif(72),
`5` = 100 * runif(72),
`6` = 150 * runif(72),
`7` = 200 * runif(72),
check.names = FALSE
)
library(ggalign)
#> Loading required package: ggplot2
ggstack(TotalPercentage) +
# add color bar plot
# we transform the input data frame into a long format data frame
ggalign(action = plot_action(
data = function(x) {
ans <- tidyr::pivot_longer(x,
cols = as.character(1:7),
names_to = "group"
)
dplyr::summarise(
ans,
value = sum(value), .by = c(`Participant ID`, group, .y)
)
}
)) +
geom_col(aes(value, .y, fill = group),
orientation = "y",
position = position_fill()
) +
scale_fill_brewer(palette = "Dark2") +
# add dendrogram
align_dendro(data = ~ .x[-1L]) &
scale_x_continuous(expand = expansion()) &
theme(plot.margin = margin(l = 5, r = 10))
Created on 2024-10-21 with reprex v2.1.0 ~