2012-06-07 17 views
31

私は水平ドットプロット(?)をggplot2に書いていました。水平バープロットを作成しようと考えました。しかし、私はこれを行うにはいくつかの制限があることを見出しています。ここでggplot2の水平バープロット

は私のデータです:

df <- data.frame(Seller=c("Ad","Rt","Ra","Mo","Ao","Do"), 
       Avg_Cost=c(5.30,3.72,2.91,2.64,1.17,1.10), Num=c(6:1)) 
df 
str(df) 

は当初、私は次のコードを使用してドットプロットを生成:

require(ggplot2) 
ggplot(df, aes(x=Avg_Cost, y=reorder(Seller,Num))) + 
    geom_point(colour="black",fill="lightgreen") + 
    opts(title="Avg Cost") + 
    ylab("Region") + xlab("") + ylab("") + xlim(c(0,7)) + 
    opts(plot.title = theme_text(face = "bold", size=15)) + 
    opts(axis.text.y = theme_text(family = "sans", face = "bold", size = 12)) + 
    opts(axis.text.x = theme_text(family = "sans", face = "bold", size = 12)) 

しかし、私は今、そのI水平barplotと発見を作成しようとしていますそうすることができません。私はcoord_flip()を試しましたが、それは役に立ちませんでした。

ggplot(df, aes(x=Avg_Cost, y=reorder(Seller,Num))) + 
    geom_bar(colour="black",fill="lightgreen") + 
    opts(title="Avg Cost") + 
    ylab("Region") + xlab("") + ylab("") + xlim(c(0,7)) + 
    opts(plot.title = theme_text(face = "bold", size=15)) + 
    opts(axis.text.y = theme_text(family = "sans", face = "bold", size = 12)) + 
    opts(axis.text.x = theme_text(family = "sans", face = "bold", size = 12)) 

誰もがggplot2に水平barplotを生成する方法についていくつかの支援を提供することができますか?

答えて

80
ggplot(df, aes(x=reorder(Seller, Num), y=Avg_Cost)) + 
    geom_bar(stat='identity') + 
    coord_flip() 

なしstat='identity' ggplotは、データをカウントに集計したいとします。

+1

ggplot2のすべての 'geom'にはデフォルトの' stat'があります。 'geom_bar'のデフォルトのstatは' bin'ですので、Justinが示したように 'identity'に変更する必要があります。 binにデフォルト設定されている他の2つのgeomは 'freqpoly'ともちろん' histogram'です。 – Pete