2016-07-11 7 views
0

私はこのように見えるデータフレーム「カウント」があります。barplot複数の集約

#     Date  Code Number_of_events 
#1     01-04 022003  5 
#2     01-06 022003  9 
#3     01-08 022003  3 
#4     01-11 022003  2 
#5     01-13 022003  5 
#... 
#3754    03-11 396001  4 
#3755    03-16 396001  4 
#3756    03-21 396001  17 
#3757    03-22 396001  23 
#3758    03-23 396001  3 

私は日付とコードによる凝集DFの結果として得た:私がやりたい

count<-aggregate(.~ Date+Code,data=df,FUN=sum) 

を各コードがシリーズ(したがって色付きのバー)であるイベント(y)対日付(x)の数の棒グラフ。イベントがまったく同じ日に起こらないことを知っている。

誰でも私にこれを手伝ってもらえますか?あなたは

答えて

1

あなたは便利geom_bar()機能が含まれているggplot2パッケージを、使用することができます使用してbarplot

barplot(xtabs(Number_of_events~Code + Date, df), 
       beside = TRUE, legend = TRUE, col = c("red", "blue")) 

かを使用することができますありがとうございました。

# simulate some data 
N <- 100 
df <- data.frame(Date = sample(seq(as.Date("2000/1/1"), by = "month", length.out = 48), N, T), 
        Code = sample(c(022003, 396001, 441002), size = N, replace = T), 
        Number_of_events = rpois(N, 5)) 

#aggregate 
count <- aggregate(.~ Date+Code,data=df,FUN=sum) 

これは、プロットのためです:

library(ggplot2) 

ggplot(count, aes(x=Date, y=Number_of_events)) + 
    geom_bar(aes(fill=as.factor(Code)), color="black", stat="identity", position="stack") 

enter image description here

いくつかのデータをシミュレート

1

我々はggplot

library(dplyr) 
library(ggplot2) 
df %>% 
    group_by(Date, Code) %>% 
    summarise(Number_of_events = sum(Number_of_events)) %>% 
    ggplot(., aes(x= Date, y = Number_of_events)) + 
     geom_bar(aes(fill= factor(Code)), position = "dodge", stat = "identity") 
関連する問題