2017-09-18 1 views
0

ggplot2パッケージを使用してファセットを有するシンプルな棒グラフを作成するおもちゃのデータセットを使用します。R:ggplot2 - バーグラフに重ねて逃れ

library(ggplot2) 
library(reshape2) # to convert to long format 

databas<-read.csv(data= 
        "continent,apples,bananas 
        North America,30,20 
        South America,15,34.5 
        Europe,15,19 
        Africa,5,35") 

databaslong<-melt(databas) 

# plotting as colored bars 
ggplot(databaslong, aes(x=variable, y=value, fill=variable))+ 
    geom_col()+ 
    facet_grid(.~continent) 

をし、次を得る:

simple bar plot with facets

リンゴをバナナの上に置く方法(またはその逆)なぜディレクティブposition="stack"(またはposition="dodge")はgeom_col()または他の場所で、ここでは効果がないのでしょうか?

答えて

3

あなたの美的マッピングでは、変数の各値(つまり、apples &バナナ)はx軸に沿って独自の位置を取得し、スタックすることはありません。あなたはリンゴ&バナナは、各大陸のために積み重ねることがしたい場合は

、あなたの代わりにx=continentを指定することができます。

ggplot(databaslong, 
     aes(x = continent, y = value, fill = variable)) + 
    geom_col() 

enter image description here

関連する問題