make_cata_tables <- function(df_cata,
.Product_Name_colname,
.Attribute_Name_colname,
.Attribute_Value_colname) {
# browser()
cata_summary_table <- df_cata %>%
dplyr::rename(Product = {{.Product_Name_colname}}, Attribute = {{.Attribute_Name_colname}}, Response = {{.Attribute_Value_colname}}) %>%
dplyr::select(Product, Attribute, Response) %>%
dplyr::group_by(Product, Attribute) %>%
dplyr::summarize(Total = sum(Response)) %>%
tidyr::pivot_wider(names_from = Attribute, values_from = Total)
return(
structure(
cata_summary_table,
class = c("cata_table", "tbl_df", "tbl", "data.frame")
)
)
}
plot.cata_table <- function(cata_summary_table){
# browser()
cata_plot <- cata_summary_table %>%
tidyr::pivot_longer(cols= !Product, names_to = "Attributes", values_to = "Count") %>%
split(.$Product, drop = TRUE) %>%
purrr::imap(~{
.x %>%
ggplot2::ggplot(ggplot2::aes(x=Attributes, y = Count)) +
ggplot2::geom_col() +
ggplot2::ggtitle(unique(.x$Product)) +
ggplot2::theme(
legend.position="bottom",
legend.title = ggplot2::element_blank(),
axis.title.x = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
axis.text.x = ggplot2::element_text(angle = 90),
panel.grid.major.x = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_line(colour = "#cfcbca"),
panel.background = ggplot2::element_rect(fill = "white",
colour = "white",
size = 0.5, linetype = "solid"),
plot.title = ggplot2::element_text(hjust = 0.5))
}
)
return(c(cata_plot))
}
# library(tidyverse)
# data <- openxlsx::read.xlsx(file.path("tests", "testdata", "Study1.xlsx"))%>%
# tidyr::pivot_longer(cols = dplyr::starts_with("Attribute"),
# names_to = "Attribute_Name",
# values_to = "Attribute_Value") %>%
# dplyr::mutate(Study = "Study1")
# cata_summary_table <- make_cata_tables(data, Product_Name, Attribute_Name, Attribute_Value)
# plot(cata_summary_table)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.