2016-04-15 24 views
0

から順に積層barplotを作る私はこのようなデータフレームを持っている:は周波数

head(mydata) 
id GeoBefore GeoAfter 
1   A  A 
2   B  B 
3   A  B 
4   B  A 
5   A  A 
6   B  B 

私は2つの列、GerBeforeとGeoAfterで積み上げ棒グラフをプロットしたいのですが、私はどのように表示したいです多くのA、B、等...各バー

である私がやろうとしています:

gb <- cbind(mydata$GeoBefore, "2014") 
ga <- cbind(mydata$GeoAfter, "2015") 
geo <- as.data.frame(rbind(gb,ga)) 
colnames(geo) <- c("value", "period") 
head(geo) 
barplot(geo, 
    legend.text=geo$period, 
    args.legend=list(bty="n",horiz=TRUE), 
    col=brewer.pal(5,"Set1"),border="white", 
    main="Title") 

が、取得:

Error in barplot.default(geo, legend.text = geo$period, args.legend = list(bty = "n", : 
'height' must be a vector or matrix 

答えて

0

あなたはこれを行うことができます。

ans <- cbind(table(mydata$GeoBefore),table(mydata$GeoAfter)) 

colnames(ans) <- c("GeoBefore","GeoAfter") 

barplot(ans,legend.text = rownames(ans)) 

enter image description here

0

テストされていない

library(ggplot2) 
ggplot(geo, aes(x = period, fill = value)) + 
    geom_histogram() 
関連する問題