gpt4 book ai didi

r - 如何使用来自 csv 文件的实际观测值正确注释堆栈条形图?

转载 作者:行者123 更新时间:2023-12-04 02:08:50 24 4
gpt4 key购买 nike

我已经实现了接受 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 文件列表,我打算为每个文件条获取带有实际观察次数的带注释堆栈条图。我怎样才能有效地实现这一点?

这是所需情节的模型:

enter image description here

最佳答案

原始答案(预编辑/评论):

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"))

enter image description here

修改后的答案(编辑后):

根据评论/编辑,我修改了情节以在片段内包含标签:

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))

enter image description here

关于r - 如何使用来自 csv 文件的实际观测值正确注释堆栈条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40833991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com