I\'ve got a survey results for two products categories, here is the data
surveyresults<-data.frame(\"Name\"=c(\"Adam\",\"John\"),
\"Gender\"=c(\"m\",\"m\
Here, we could reshape into 'long' format and then use ggplot
to plot
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)
p1 <- surveyresults %>%
type.convert(as.is = TRUE) %>%
pivot_longer(cols = fash_pay_cash:elc_DlvTime_afternoon,
names_to = c("group", ".value"), names_pattern = "^(\\w+_\\w+)_(\\w+)$") %>%
pivot_longer(cols = cash:afternoon, values_drop_na = TRUE) %>%
group_by(group, name) %>%
summarise(value = sum(value)) %>%
ggplot(aes(x = name, y = value, fill = group)) +
geom_col(position = 'dodge') +
facet_wrap(vars(group))
ggplotly(p1)
-output
Or we can use facet_wrap_paginate
from ggforce
library(ggforce)
p1 <- surveyresults %>%
type.convert(as.is = TRUE) %>%
pivot_longer(cols = fash_pay_cash:elc_DlvTime_afternoon,
names_to = c("group", ".value"),
names_pattern = "^(\\w+_\\w+)_(\\w+)$") %>%
pivot_longer(cols = cash:afternoon, values_drop_na = TRUE) %>%
group_by(group, name) %>%
summarise(value = sum(value)) %>%
ggplot(aes(x = name, y = value, fill = group)) +
geom_col(position = 'dodge') +
facet_wrap_paginate(~ group, ncol = 1, nrow = 2, page = 2)
n <- n_pages(p1)
pdf('surveyout.pdf')
for(i in seq_len(n)) print(p1 +
facet_wrap_paginate(~ group, ncol = 1, nrow = 2, page = i))
dev.off()
Or it can be split into smaller subsets, create ggplot in each and then apply the plotly on it separately
library(purrr)
lstOut <- surveyresults %>%
type.convert(as.is = TRUE) %>%
pivot_longer(cols = fash_pay_cash:elc_DlvTime_afternoon,
names_to = c("group", ".value"),
names_pattern = "^(\\w+_\\w+)_(\\w+)$") %>%
pivot_longer(cols = cash:afternoon, values_drop_na = TRUE) %>%
group_by(group, name) %>%
summarise(value = sum(value)) %>%
ungroup %>%
group_split(grp =(as.integer(factor(group,
levels = unique(group)))%/% 3) + 1) %>%
map(~
ggplot(.x, aes(x = name, y = value, fill = group)) +
geom_col(position = 'dodge') +
facet_wrap(vars(group)) -> p)
ggplotly(lstOut[[1]])
ggplotly(lstOut[[2]])