如何在列表中保存多个饼图?
Posted
技术标签:
【中文标题】如何在列表中保存多个饼图?【英文标题】:How can I save a number of piecharts in a list? 【发布时间】:2019-10-01 12:12:30 【问题描述】:我正在尝试创建一个函数。在我给它一个带有等级的数据框后,该函数必须为我提供一个饼图列表。
我不知道出了什么问题,如果你能给我一点推动,我可以继续我的报告:)
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
我希望输出是一个列表,这样我就可以轻松地绘制带有多个饼图的图像。另一方面,输出给了我这个错误:
"seq.int(x[i], x[i + 1], length.out = n) 中的错误:'length.out' 必须是非负数”
样本数据:
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, 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, 9.4, 4.2, 5.5, 5.4, 8.9, 7.7, 9, 4.3, 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)
【问题讨论】:
您能在您的问题中添加示例数据吗? 您能解释一下如何上传示例数据文档吗?它是一个有 5 列和 132 个等级的矩阵(从 0.0 到 10.0)。 使用dput
。在您的控制台上,执行dput(<your matrix name>)
。如果重新运行,您得到的输出将再次生成矩阵。
输出编辑到问题...这样可以吗?
请使用代码块更新您的 R sn-p,而不仅仅是复制粘贴。
【参考方案1】:
数据框中的第五列和第六列属于character
类。要执行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]]
希望这很清楚!
【讨论】:
我收到以下错误:“$<-.data.frame
(*tmp*
, CALEID.TH.Caleidoscoop.theorie, value = numeric(0)) 中的错误:替换有 0 行,数据有 132”
尝试使用data[[5]]
代替实际的列名。
感谢您到目前为止的帮助,但列表 pie_charts 似乎为空...?
@J.O.P.P.您必须在函数中返回列表pie_charts
。否则您的函数将不会返回任何值。编辑了我的答案。请看一下。
感谢更新功能!但是你能告诉我如何检查列表中的内容吗?以上是关于如何在列表中保存多个饼图?的主要内容,如果未能解决你的问题,请参考以下文章