R ggplot-ggsave:为具有不同数量变量的组图创建动态宽度/高度

R ggplot-ggsave:为具有不同数量变量的组图创建动态宽度/高度,r,ggplot2,ggsave,R,Ggplot2,Ggsave,我已经在这个话题上工作了几天,但没有任何进展。我需要运行一个脚本来绘制来自调查的几个所谓的项目组(项目电池)。剧本终于成功了,但情节并不十分美观。无需用户输入高度/宽度ggsave即可接管控制并绘制“一刀切”样式。这不是很令人满意,因为在我的例子中,项目电池包含不同数量的项目(变量)和刻度(0-1,1-5)。因此,钢筋有时较厚/较薄。 是否有任何选项可以避免这种行为,并允许ggplot(或ggsave?)选择“正确”的尺寸(例如,不仅条形图具有相同的宽度,而且绘图也会改变与所需绘图空间相关的尺寸

我已经在这个话题上工作了几天,但没有任何进展。我需要运行一个脚本来绘制来自调查的几个所谓的项目组(项目电池)。剧本终于成功了,但情节并不十分美观。无需用户输入高度/宽度
ggsave
即可接管控制并绘制“一刀切”样式。这不是很令人满意,因为在我的例子中,项目电池包含不同数量的项目(变量)和刻度(0-1,1-5)。因此,钢筋有时较厚/较薄。 是否有任何选项可以避免这种行为,并允许
ggplot
(或
ggsave
?)选择“正确”的尺寸(例如,不仅条形图具有相同的宽度,而且绘图也会改变与所需绘图空间相关的尺寸)? 非常感谢

以下是起草所述问题的示例数据集:

##create random data
          surveydata <- as.data.frame(replicate(10,sample(0:1,200,rep=TRUE)))
          names(surveydata)[] <- paste("Q", 1:length(surveydata), sep="")
          
          # change values of columns
          surveydata[4:10] <-  apply(surveydata, 2, function (x) sample(5, size = nrow(surveydata), replace = TRUE))
          
          #create column with same distribution of values
          surveydata$group <- c("survey1","survey2")
          surveydata$group <- as.factor(surveydata$group)

## create vector with variable names
          itemgroupA <- data.frame(names(surveydata)[c(1, 2, 3)])
          itemgroupA$itemgroup <- "itemgroupA"
          names(itemgroupA)[1] <- "variables"
          itemgroupB <- data.frame(names(surveydata)[c(-1, -2, -3, -11)])
          itemgroupB$itemgroup <- "itemgroupB"
          names(itemgroupB)[1] <- "variables"
          
          itemgroups <- rbind(itemgroupA, itemgroupB)
          uniq <- unique(itemgroups$itemgroup)
          
## start LOOP
          for (i in 1:length(uniq)) {
                    j_tab <- subset(itemgroups, itemgroup == uniq[i]) #subset by item battery
                    #j_tab <- subset(code_itemgroups, item_groups == "F2200") #subset by item battery
                    j_vec <- as.character(j_tab$variables) #vector with names of variables for each item battery
                    
                    #old basetab
                    basetab <- subset(surveydata, select = c("group", j_vec)) #subset dataset by vector (item battery)
                    
                    #aggregation table
                    sumtab <- basetab %>% 
                              group_by(group) %>% 
                              summarise(across(everything(), mean, na.rm = TRUE)) #grouping variable wird nicht berücksichtigt.
                    #transpose
                    sumtab <- reshape2::melt(sumtab, id.vars="group")
                    
          
                    #axis min-max
                    y_high <- as.numeric(max(basetab[c(-1)], na.rm=T))
                    y_low <- as.numeric(min(basetab[c(-1)], na.rm=T))
                    
                    #plot
                    p <- ggplot(sumtab, aes(x=variable, y=value, fill=factor(group))) +
                              geom_bar(stat="identity",
                                       position = position_dodge()) +
                              scale_y_continuous(expand = c(0,0)) +
                              coord_cartesian(ylim = c(y_low, y_high)) +
                              theme_minimal()
                    
                    ggsave(p,filename=paste("plot_", uniq[i],".pdf",sep=""))
                    
          }
##创建随机数据
调查数据