2016-11-27 3 views
1

私はdata.frameのリストを入力として受け入れる関数を実装し、次にしきい値でフィルタを外しました。これで、フィルタ結果をCSVファイルとしてエクスポートできます。出力をより良く理解するために、各スタックでの観測の数、アノテートされたスタックバープロットの取得が良いオプションになる可能性があります。どのように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

+0

私はあなただけで作られた編集前に、この上で開始しました。大切ですか? –

+0

10時間前とほぼ同じ質問にすばらしい答えがありますが、注釈を試したことはありますか? – Nate

+0

@ Hack-R編集は重要ではありません。入力リストが十分に大きいことを確認してください。あなたの懸念をありがとう:) – Jerry

答えて

4

オリジナル回答(編集前/コメント):

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

修正回答(ポスト-edit):

コメント/編集に基づいて、私は、セグメントの内側にラベルを含むようにプロットを改訂しています

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

+0

各セグメントの実際の観測数に明示的に注釈を付けるにはどうすればよいですか? – Jerry

+0

@ Jerry.Shadああ、それはy軸のラベルです。ラベルをどこか別にしたいですか?もしそうなら、それは問題ありませんが、私は今すぐ洗濯物を折り畳む必要があり、まもなくそれに戻ってきます。 –

+0

私は希望のプロットのモックアップを追加しました。どのように観測の数が示されているそのプロットを得ることができますか?ありがとうございます:) – Jerry

関連する問題