如何在列表中保存多个饼图?

How can I save a number of piecharts in a list?

我正在尝试做一个函数。在我给它一个带有等级的数据框之后,该函数必须为我提供一个饼图列表。

我看不出出了什么问题,如果你能给我一点推动,这样我就可以继续我的报告了:)

function_pie.chart<- function(data) {
   pie_charts <- list()
   for(i in 1:ncol(data)) {

       slices <- c(count_if(lt(5.5), data[,i]), count_if(gt(5.4),data[,i]))
       lbls <- c("Passed", "Failed")
       pct <- round(slices/sum(slices)*100)
       lbls <- paste(lbls, pct)
       lbls <- paste(lbls,"%",sep="")
       p1 <- eval(substitute(pie(slices,labels = lbls, 
                           col=rainbow(length(lbls)),
                           main=colnames(data[,i]))))
       pie_charts[[i]] <- p1
     }
}

我希望输出是一个列表,这样我就可以轻松地绘制包含多个饼图的图像。另一方面,输出给了我这个错误:

"Error in seq.int(x[i], x[i + 1], length.out = n) : 'length.out' must be a non-negative number"

示例数据:

data <- data.frame(
`BSTAT-THI Beschrijvende Statistiek` = c(7.3, 6.3, 5.8, 6.9, 5.7, 8.9, 6.1, 7, 8.1, 7.1, 6.2, 6.5, 7, 4.9, 9, 6.2, 5.6, 7.3, 6.6, 7.7, 8.3, 7.3, 7.1, 5.3, 9.3, 7.4, 6.6, 6.3, 7, 5.7, 6.7, 6.9, 6.9, 6.6, 5.3, 6.5, 5.7, 7.5, 6.3, 6.8, 7.3, 5.9, 7.1, 6.9, 7, 7.6, 7, 7.1, 6.5, 5.2, 5.8, 6.2, 6.4, 6.7, 5.3, 7.3, 7, 7.3, 8.6, 6.2, 5.8, 5.8, 6.6, 7.1, 6, 9.5, 7.3, 8, 7.3, 6.5, 9, 8.2, 7.3, 6.5, 7.8, 7.4, 6.9, 7.3, 6.2, 7.5, 7.6, 7.1, 6.3, 6.9, 7.5, 7.7, 8.3, 7.4, 5.2, 7, 6.6, 6.5, 7.5, 6.3, 8.1, 6.9, 6.8, 8.1, 7.4, 7.2, 9.2, 7.8, 6.6, 6.1, 8.3, 6.2, 9.2, 7.4, 9, 8.6, 7.1, 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, 5.4, 4.7, 6.4, 8.5, 7.1, 8.2, 6.5, 7.2),
`CALEID-TH|Caleidoscoop theorie` = c("7.4000000000000004", "6.2999999999999998", "5.7000000000000002", "7.0999999999999996", "6.0999999999999996", "7.5", "5.5", "6", "5.0999999999999996", "6.5999999999999996", "6.2999999999999998", "6.5", "4.9000000000000004", "NA", "8.5", "6.9000000000000004", "3.2000000000000002", "7.2999999999999998", "4.2999999999999998", "4.7000000000000002", "7.9000000000000004", "8.6999999999999993", "5.2999999999999998", "6", "6.4000000000000004", "5.7999999999999998", "3.8999999999999999", "5.7999999999999998", "7.7000000000000002", "4.7999999999999998", "5.7000000000000002", "7.2000000000000002", "5.2999999999999998", "5.7999999999999998", "5.7999999999999998", "5.5", "5.7000000000000002", "8.9000000000000004", "6.4000000000000004", "5.7999999999999998", "4.7000000000000002", "5", "5.7000000000000002", "4.9000000000000004", "5.0999999999999996", "6", "5.7999999999999998", "5.5999999999999996", "6", "6.2999999999999998", "6.9000000000000004", "6.2000000000000002", "5", "5.5999999999999996", "NA", "8.5999999999999996", "4.2999999999999998", "7", "8.9000000000000004", "5.2000000000000002", "5.2000000000000002", "6", "5.5", "6.7999999999999998", "3.6000000000000001", "5.9000000000000004", "8.9000000000000004", "4.4000000000000004", "6.2000000000000002", "6.2000000000000002", "6.2000000000000002", "5.2999999999999998", "5.5999999999999996", "6", "7.5999999999999996", "6.5", "7.5", "4.2999999999999998", "6.2000000000000002", "8.5999999999999996", "6", "4.7000000000000002", "3.6000000000000001", "8.4000000000000004", "8.9000000000000004", "6.2999999999999998", "8.1999999999999993", "8.8000000000000007", "6.5999999999999996", "4.4000000000000004", "8", "6.5999999999999996", "6.0999999999999996", "6.0999999999999996", "8", "6.2000000000000002", "5.2999999999999998", "6.2000000000000002", "5.7999999999999998", "7.4000000000000004", "6.5", "8", "6.9000000000000004", "4.5", "7.5999999999999996", "6.0999999999999996", "6.0999999999999996", "4.7999999999999998", "6", "5.5999999999999996", "5", "5.4000000000000004", "4.4000000000000004", "5.4000000000000004", "6.2999999999999998", "7.4000000000000004", "9", "6.2000000000000002", "5.5", "6.2000000000000002", "7.7999999999999998", "6", "7", "5.9000000000000004", "6.2000000000000002", "4.2999999999999998", "7.0999999999999996", "5.4000000000000004", "7.5999999999999996", "5", "5.7999999999999998", "6.9000000000000004"),
`COVA1-PRI Communicatieve vaardigheden 1` = c("7", "6", "6", "7.5", "5.5", "7.5", "6", "8", "7.5", "7.5", "6", "6", "7", "6.5", "8", "6.5", "NA", "6.5", "6", "7.5", "7", "7.5", "5.5", "6.5", "6.5", "5", "6", "6", "7", "6", "7.5", "6", "5", "7.5", "6.5", "7.5", "NA", "7.5", "7", "8.5", "8", "6", "7", "8", "6.5", "7.5", "7", "5", "7.5", "6.5", "7.5", "8", "7.7000000000000002", "5.5", "6", "7.5", "7.5", "6.5", "7.5", "6.5", "6", "5.5", "6", "6", "5.5", "6", "8", "6.5", "7.5", "5.5", "6", "7", "8", "8.5", "7.5", "8", "7", "7.5", "8.5", "7.5", "8.5", "7.5", "5.5", "7.5", "7.5", "6", "6.5", "7", "7", "8", "6.5", "7", "8", "NA", "6", "8", "6.5", "8", "7.5", "6.5", "6", "7.5", "7.5", "6.5", "7.5", "6.5", "6.5", "6.5", "6", "8", "6.5", "6", "6", "6", "7", "6", "7.5", "6.5", "NA", "8", "6.5", "7.5", "7", "6", "6", "6.5", "6", "6", "6.5", "6.5", "6", "8"),
stringsAsFactors = FALSE)

数据框中的第五列和第六列是 character class。要执行 length 等数字运算,您需要传递一个 numeric 值。此外,第五列中的某些行具有 'NA' 值。我们还需要处理它。

对您的数据框执行以下操作。

# Convert NA values to 0
data[data=="NA"] <- 0

# Convert columns from character to numeric
data[[5]] <- as.numeric(data[[5]])
data[[6]] <- as.numeric(data[[6]])

编辑: 刚刚发现您无法将饼图对象保存到变量以供以后使用。您的一种可能是使用提供的方法 here。另一种选择是改用 plotly

# Updated function using plotly

library(plotly)

plotfunc <- function(data) {
    pie_charts <- list()
    for(i in 1:ncol(data)) {

        slices <- c(count_if(lt(5.5), data[,i]), count_if(gt(5.4),data[,i]))
        lbls <- c("Passed", "Failed")
        pct <- round(slices/sum(slices)*100)
        lbls <- paste(lbls, pct)
        lbls <- paste(lbls,"%",sep="")
        pie_charts[[i]] <- plot_ly(labels = lbls, values = slices, type = 'pie')
    }
    pie_charts
}

# Pass the dataframe to the function

plots <- plotfunc(data)

# The 'plots' array will have individual plots
# This will print the first plot
plots[[1]]

# The second...and so on
plots[[2]]

希望这是清楚的!