2017-05-10 12 views
2

私は 'ggplot2'を使ってスタックバープロットを生成した次のデータセットを持っています。RベースプロットのR ggplot2バー相当物

データ - >

X1 X2 X3 
1 1 10 13.53 
2 1 10 16.81 
3 2 10 16.24 
4 2 10 17.42 
5 1 10 15.20 
6 1 10 29.40 
7 1 10 45.30 
8 1 10 14.00 
9 1 10 23.50 
10 2 10 12.30 

ggplot2コード--->

ggplot(data=dat1, aes(x=X2, y=X3, fill=X1,width=0.5)) + 
    geom_bar(stat="identity") + coord_flip() + 
    theme(legend.position="bottom",legend.direction="horizontal",legend.text=element_text(size=12), 
     plot.margin=unit(c(0.1,1,0.25,1), "cm"), 
     panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.title.y=element_blank(), 
     axis.text.y=element_blank(), 
     axis.ticks.y=element_blank())+ 
    guides(fill=guide_legend(nrow=1,byrow=TRUE)) + 
    scale_x_continuous(breaks = 10)+ 
    scale_y_continuous(breaks=c(seq(0,sum(dat1$X3),50),sum(dat1$X3)),expand = c(0,0))+ 
    labs(fill="") 

私は今(barplotを使用してRベースプロットに変換したい)Rの機能するように、私はそれをどこか別の場所で使うことができます。私はスタックバープロットを私は単一の列を使用して2つのグループを使用してスタックする必要があるベースバープロット関数を使用して生成することができません。

ありがとうございました。

+0

ggplot barplotを別の場所で使用できないのはなぜですか? – user3640617

+0

@ user3640617:結合に問題がある別のRプロット(時系列)と結合する必要があります。代替案として、私はRベースプロットで両方を作りたがっていました。 –

答えて

2

多分これはあなたに

X1=c(1,1,2,2,1,1,1,1,1,2) 
X2=rep(10,10) 
X3=c(13.53,16.81,16.24,17.42,15.20,29.40,45.30,14.00,23.50,12.30) 

my.color<-rev(c("#4292C6","#08306B")) 

# this is the important thing, to pass your data into barplot as a matrix 
dat1<-as.matrix(X3,X2) 

barplot(dat1,col = my.color[as.factor(X1)],border = my.color[as.factor(X1)], 
     beside = FALSE,horiz=TRUE, 
     xaxt="n",yaxt="n",xlab="",ylab="",main="") 

axis(1, at=seq(0,sum(X3),by=50)) 
axis(2, at=0.7,labels = 10) 
legend(0,1.4, fill=my.color, legend=c(1, 2))  

を助けることができる結果は以下の通りです:

enter image description here

あなたは多分XPDオプションを変更する必要がありますプロットの外に凡例を追加するにはpar()のFALSEからTRUEへ:

par(xpd=TRUE) 

希望します。

+0

ありがとう..これは私を助けます.. –