Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中组合tableGrob和ggplot对象时更改填充_R_Ggplot2_Png_Data Visualization_Gridextra - Fatal编程技术网

在R中组合tableGrob和ggplot对象时更改填充

在R中组合tableGrob和ggplot对象时更改填充,r,ggplot2,png,data-visualization,gridextra,R,Ggplot2,Png,Data Visualization,Gridextra,我正在尝试将多个tableGrob对象与ggplot对象合并到一个.png文件中;我很难理解如何编辑tableGrob主题参数,使我能够调整表格对象的填充和尺寸。理想情况下,我希望它们位于一个4*1的网格中,每个网格之间的填充最小。表格对象的文本也应该左对齐 我正在使用虚拟数据,输入数据集的每一行都将用于创建自己的.png文件(下面的代码片段中包含两行,以生成一个可复制的示例) 我试图以此为例,根据每个表对象的“高度”属性设置grid.arrange间距,但这并没有完全奏效。作为旁注,现在,绘图

我正在尝试将多个tableGrob对象与ggplot对象合并到一个.png文件中;我很难理解如何编辑tableGrob主题参数,使我能够调整表格对象的填充和尺寸。理想情况下,我希望它们位于一个4*1的网格中,每个网格之间的填充最小。表格对象的文本也应该左对齐

我正在使用虚拟数据,输入数据集的每一行都将用于创建自己的.png文件(下面的代码片段中包含两行,以生成一个可复制的示例)

我试图以此为例,根据每个表对象的“高度”属性设置grid.arrange间距,但这并没有完全奏效。作为旁注,现在,绘图每次都会被覆盖;我稍后会解决这个问题,我只关心如何正确地获得输出尺寸/排列。代码如下;编辑以包含库调用,并修复了一个打字错误:

require("ggplot2")
require("gridExtra")
require("grid")

    # Generate dummy data frame
    sampleVector <- c("1", "Amazing", "Awesome", "0.99", "0.75", "0.5", "$5,000.00", "0.55", "0.75", "0.31", "0.89", "0.25", "Strong community support", "Strong leadership", "Partners had experience", "", "CBO not supportive", "Limited experience", "Limited monitoring", "")
    sampleVectorB <- c("3", "Amazing", "Awesome", "0.99", "0.75", "0.5", "$5,000.00", "0.55", "0.75", "0.31", "0.89", "0.25",  "Strong community support", "Strong leadership", "Partners had experience", "", "CBO not supportive", "Limited experience", "Limited monitoring", "")

    sampleDF <- data.frame(rbind(sampleVector, sampleVectorB))
    colnames(sampleDF) <- c("CBO", "PMQ", "HMQ", "ER", "PR", "HR", "NS", "CTI", "Home and Hosp", "Home", "Phone", "Other", "S1", "S2", "S3", "S4", "C1", "C2", "C3", "C4")

    indata <- sampleDF

    #Finds the longest string from a vector of strings (i.e. #chars, incld. whitespace); returns index into this vector that corresponds to this string
    findMax <- function(tempVector){
         tempMaxIndex <- 1

         for(i in 1:length(tempVector)){
              print(nchar(tempVector[i]))
              if(nchar(tempVector[i]) > nchar(tempVector[tempMaxIndex])){
                   tempMaxIndex <- i
              }
         }
         return(tempMaxIndex)
    }

    # Same as above but w/o the colon:
    addWhitespacePlain <- function(stringVec, maxNum){
         for(i in 1:length(stringVec))
         {
              string <- stringVec[i]

              while(nchar(string) < maxNum+1){
                   string <- paste(string, " ")
              }

              stringVec[i] <- string
         }

         return(stringVec)
    }


    staticText <- c("Participant Match Quality", "Hospital-Level Match Quality", "Enrollment Rate", "Participant Readmissions", "Hospital Readmissions", "Net Savings", 
                    "Strengths", "Challenges")
    m <- findMax(staticText)
    staticText <- addWhitespacePlain(staticText, nchar(staticText[m]))

    # Loop through our input data and keep only one CBO each pass
    for(i in 1:length(indata$CBO)){

         # Select only the row that has this CBO's data
         temp <- indata[i,]

         ###############################################################################################

         # MAKE TOP TEXT TABLE (as a DF)

         # Get values from our input data set to fill in the values for the top text portion of the graphic
         topVals <- t(data.frame(temp[2], temp[3], temp[4], temp[5], temp[6], temp[7]))
         topDF <- data.frame(staticText[1:6], topVals, row.names=NULL)
         colnames(topDF) <- c(paste("CBO", temp[1]), " ")

         # Find which of the strings from the top text portion is the longest (i.e. max # of chars; including whitespace)
         m2 <- findMax(rownames(topDF)) # returns an index into the vector; this index corresponds to string w/max num of chars

         # Add whitespace to non-max strings so all have the same length and also include colons
         rownames(topDF) <-  addWhitespacePlain(rownames(topDF), nchar(rownames(topDF)[m2]))

         # for testing
       #  print(topDF, right=FALSE)

         ###############################################################################################

         # MAKE BAR CHART

         #Subset the data to select the vars we need for the horizontal bar plot
         graphdata <- t(data.frame(temp[,8:12]))
         vars <- c("CTI", "Home & Hosp.", "Home", "Phone", "Other")
         graphDF <- data.frame(vars, graphdata, row.names = NULL)
         colnames(graphDF) <- c("vars", "values")

         # Make the plot (ggplot object)
         barGraph <- ggplot(graphDF, aes(x=vars, y=values,fill=factor(vars))) +
         geom_bar(stat = "identity") +
         theme(axis.title.y=element_blank())+
         theme(legend.position="none")+
         coord_flip()

        # print(barGraph)

         ###############################################################################################

         # MAKE BOTTOM TEXT TABLE

         strengths <- t(data.frame(temp[13], temp[14], temp[15], temp[16]))
         challenges <- t(data.frame(temp[17], temp[18], temp[19], temp[20]))

         #Drop nulls
         strengths <- data.frame(strengths[which(!is.na(strengths)),], row.names=NULL)
         challenges <- data.frame(challenges[which(!is.na(challenges)),], row.names=NULL)

         colnames(strengths) <- c(staticText[7])
         colnames(challenges) <- c(staticText[8])

         ###############################################################################################

         # OUTPUT (padding not resolved yet)

         # Set the path for the combined image
         png("test1", height=1500, width=1000)

       #customTheme <- ttheme_minimal(core=list(fg_params=list(hjust=0, x=0.1)),
       #                           rowhead=list(fg_params=list(hjust=0, x=0)))

       # top<-tableGrob(topDF, theme=customTheme)
       # bottom_strength <- tableGrob(strengths, theme=customTheme)
       # bottom_challenges <- tableGrob(challenges, theme=customTheme)

       top<-tableGrob(topDF)
       bottom_strength <- tableGrob(strengths)
       bottom_challenges <- tableGrob(challenges)

        x <- sum(top$heights)
        y <- sum(bottom_strength$heights)
        z <- sum(bottom_challenges$heights)

         grid.arrange(top, barGraph, bottom_strength, bottom_challenges,
                     as.table=TRUE,
                    heights=c(2, 1, 2, 2),
                    nrow = 4)
                   # heights= unit.c(x, unit(1), y, z))
         dev.off()
    }
require(“ggplot2”)
要求(“额外”)
要求(“网格”)
#生成虚拟数据帧

sampleVector你的例子太复杂了,我不确定到底是什么问题。下面的4x1布局是零填充的,这就是你想要的吗

ta <- tableGrob(iris[1:4,1:2])
tb <- tableGrob(mtcars[1:3,1:3])
tc <- tableGrob(midwest[1:5,1:2])
p <- qplot(1,1) + theme(plot.background=element_rect(colour = "black"))
h <- unit.c(sum(ta$heights), unit(1,"null"), sum(tb$heights), sum(tc$heights))
grid.newpage()
grid.arrange(ta,p,tb,tc, heights=h)

ta代码不运行<代码>库()
调用丢失、键入错误和错误消息。请重新检查您的代码并提供MWE。