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,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=""))
                    
          }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)