Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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 如何使用csv文件中的实际观察数正确注释堆栈条形图?_R_Csv_Ggplot2 - Fatal编程技术网

R 如何使用csv文件中的实际观察数正确注释堆栈条形图?

R 如何使用csv文件中的实际观察数正确注释堆栈条形图?,r,csv,ggplot2,R,Csv,Ggplot2,我实现了一个函数,它接受data.frame列表作为输入,然后按阈值过滤掉。现在我可以将过滤结果导出为csv文件。为了更好地理解输出,每个输出中有多少观测值,获取带注释的堆栈条形图可能是一个不错的选择。如何获得csv文件列表的注释条形图?有人能给我一些可能的想法来达到我想要的结果吗?如何操作csv文件以获取堆栈条形图?有什么想法吗?非常感谢 可复制数据: output <- list( bar = data.frame(begin=seq(2, by=14, len=45), end=

我实现了一个函数,它接受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))
)
输出原始答案(预编辑/评论):

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

d在您刚刚编辑之前,我就开始了。重要吗?你对10小时前的一个几乎相同的问题有一个很好的答案,你尝试了什么注释?@Hack-R edit并不重要,只是确保输入列表足够大。谢谢你的关心:)当然。顺便说一句,我要确认的是,我应该只使用带有“保存”字样的csv,对吗?@Hack-R使其更具动态性是非常值得赞赏的。如何明确标注每个片段的实际观察次数?@Jerry.Shad哦,这就是y轴标签。你想把标签放在别处吗?如果是这样,那很好,但我现在需要叠一些衣服,很快就会回来。我刚刚添加了我想要的情节模型。我怎样才能得到观测数也指示的图呢?非常感谢:)@Jerry.Shad好的,我现在回来了。我将为您添加这一点,但请注意,使用y轴作为obs编号的标签可能会更好,因为条形段的高度可变。下面是tho:@Jerry.Shad我更新答案的方式。我不知道什么是更兼容,但如果你想避免循环,你可以把它变成一个函数,然后使用apply。在RStudio中,您甚至不需要编写代码来完成此操作,只需突出显示一个,然后单击魔杖“提取函数”。
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))