我已经实现了接受data.frame列表作为输入,然后按阈值过滤掉的功能。现在,我可以将过滤后的结果导出为csv文件。为了更好地了解每个输出中有多少观察点,最好选择带注释的堆栈条形图。如何获取带注释的csv文件列表的条形图?谁能给我可能的想法以实现所需的输出?如何处理csv文件以获取堆栈条图?任何想法 ?非常感谢
可复制的数据:
output <- list(
bar = data.frame(begin=seq(2, by=14, len=45), end=seq(9, by=14, len=45), score=sample(60,45)),
cat = data.frame(begin=seq(5, by=21, len=36), end=seq(13, by=21, len=36), score=sample(75,36)),
foo = data.frame(begin=seq(8, by=18, len=52), end=seq(15, by=18, len=52), score=sample(100,52))
)
我实现了此功能,以按阈值过滤输入列表:
myFunc <- function(mList, threshold) {
# check input param
stopifnot(is.numeric(threshold))
res <- lapply(mList, function(elm) {
split(elm, ifelse(elm$score >= threshold, "saved", "droped"))
})
rslt <- lapply(names(res), function(elm) {
mapply(write.csv,
res[[elm]],
paste0(elm, ".", names(res[[elm]]), ".csv"))
})
return(rslt)
}
#' @example
myFunc(output, 10)
现在,我得到了csv文件的列表,我打算获取带有实际观察次数的每个文件栏的带注释的堆栈栏图。我如何才能有效地做到这一点?
这是所需情节的模型:
原始答案(预编辑/评论):
d <- dir()[grepl("\\.droped", dir())]
s <- dir()[grepl("\\.saved", dir())]
dropped <- as.numeric()
for(i in d){
dropped <- c(dropped,nrow(read.csv(i)))
}
saved <- as.numeric()
for(i in s){
saved <- c(saved,nrow(read.csv(i)))
}
tmp1 <- cbind(dropped,saved)
# Stacked Bar Plot with Colors and Legend
barplot(tmp1, main="CSV File Row Counts",
xlab="Number of Obs.", col=c("darkblue","red", "green"),
legend = c("cat", "bar", "foo"))
修改后的答案(修改后):
根据评论/编辑,我对图表进行了修改,以在各段中包含标签:
require(ggplot2)
Data <- data.frame(obs = c(tmp,tmp0),
# could get name from "output" to make it programmatic:
name = c("cat", "foo", "bar"),
filter = c(rep("Dropped",length(dropped)),
rep("Saved", length(saved)))
)
ggplot(Data, aes(x = filter, y = obs, fill = name, label = obs)) +
geom_bar(stat = "identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句