私はこれと同様の問題を抱えています:スタックチャートとグループ化されたチャートggplot2を組み合わせる
私は次のようなデータを持っています:
私が作成したいのgenom_bar
はggplot
、次のようなグラフです。
青い領域はのgender
一部を表しage_group
ます。私のコードは次のようになります:
p <- ggplot(data, aes(x=`Green cars are nice`, y=round(percentage, digits = 2), fill = Age_group)) +
geom_bar(stat = "identity", position=position_dodge()) +
facet_grid(.~residence) +
xlab(filename) +
ylab(NULL) +
theme_bw() +
theme(axis.text.x = element_text(size = 12, angle = 45, vjust = 1, hjust=1)) +
theme(
axis.title.x = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 14),
axis.text.y = element_text(size = 14)) +
guides(fill=guide_legend(title=NULL))
p
ペイントで作成した画像でバーを表示するには、何を追加する必要がありますか?投稿したSOの例に示されているコードを理解するのに苦労しました。
編集:
Berlin 18-39 Woman Agree 1.689550205 0.009283243
Berlin 18-39 Woman Disagree 3.942283812 0.0216609
Berlin 18-39 Woman Don't know 0.563183402 0.003094414
Berlin 40-70 Woman Agree 1.689550205 0.009283243
Berlin 40-70 Woman Disagree 3.942283812 0.0216609
Berlin 40-70 Woman Don't know 0.563183402 0.003094414
London 18-39 Woman Agree 1.689550205 0.009283243
London 18-39 Woman Disagree 3.942283812 0.0216609
London 18-39 Woman Don't know 0.563183402 0.003094414
London 40-70 Woman Agree 1.689550205 0.009283243
London 40-70 Woman Disagree 3.942283812 0.0216609
London 40-70 Woman Don't know 0.563183402 0.003094414
Berlin 18-39 Man Agree 1.689550205 0.009283243
Berlin 18-39 Man Disagree 3.942283812 0.0216609
Berlin 18-39 Man Don't know 0.563183402 0.003094414
Berlin 40-70 Man Agree 1.689550205 0.009283243
Berlin 40-70 Man Disagree 3.942283812 0.0216609
Berlin 40-70 Man Don't know 0.563183402 0.003094414
London 18-39 Man Agree 1.689550205 0.009283243
London 18-39 Man Disagree 3.942283812 0.0216609
London 18-39 Man Don't know 0.563183402 0.003094414
London 40-70 Man Agree 1.689550205 0.009283243
London 40-70 Man Disagree 3.942283812 0.0216609
London 40-70 Man Don't know 0.563183402 0.003094414
の設計はggplot
、プロットに埋め込む必要のある変数の数についてかなり意見が分かれています。したがって、これはあなたが求めた方法では実行できません。
A few things to consider, none of them exactly what you asked for, none of them are necessarily good visualizations.
Here's your data (I changed the names so you'll need to adjust them to those on your local setup). Also, "Don't know" was codified as "Don't".
df <- read.delim(text=
"residence age sex agree persons percent
Berlin 18-39 Woman Agree 1.689550205 0.009283243
Berlin 18-39 Woman Disagree 3.942283812 0.0216609
Berlin 18-39 Woman Don't 0.563183402 0.003094414
Berlin 40-70 Woman Agree 1.689550205 0.009283243
Berlin 40-70 Woman Disagree 3.942283812 0.0216609
Berlin 40-70 Woman Don't 0.563183402 0.003094414
London 18-39 Woman Agree 1.689550205 0.009283243
London 18-39 Woman Disagree 3.942283812 0.0216609
London 18-39 Woman Don't 0.563183402 0.003094414
London 40-70 Woman Agree 1.689550205 0.009283243
London 40-70 Woman Disagree 3.942283812 0.0216609
London 40-70 Woman Don't 0.563183402 0.003094414
Berlin 18-39 Man Agree 1.689550205 0.009283243
Berlin 18-39 Man Disagree 3.942283812 0.0216609
Berlin 18-39 Man Don't 0.563183402 0.003094414
Berlin 40-70 Man Agree 1.689550205 0.009283243
Berlin 40-70 Man Disagree 3.942283812 0.0216609
Berlin 40-70 Man Don't 0.563183402 0.003094414
London 18-39 Man Agree 1.689550205 0.009283243
London 18-39 Man Disagree 3.942283812 0.0216609
London 18-39 Man Don't 0.563183402 0.003094414
London 40-70 Man Agree 1.689550205 0.009283243
London 40-70 Man Disagree 3.942283812 0.0216609
London 40-70 Man Don't 0.563183402 0.003094414
")
You could try to use color to codify this. The plot is quite bad and I bet you could potentially find a combination of colors
and fills
that is less disgusting. I wouldn't recommend this to my worst enemy but be my guest.
ggplot(df,
aes(x=agree,
y=round(percent, digits = 2),
fill = age,
color=sex)) +
scale_fill_manual(values=c("gray50", "white"))+
scale_color_manual(values=c("red", "black"))+
geom_bar(stat = "identity",
position=position_dodge(width = 1)) +
facet_grid(.~residence)
Another option is creating an interaction variable. But you don't get to dodge and stack.
ggplot(df,
aes(x=agree,
y=round(percent, digits = 2),
fill = interaction(sex, age))) +
scale_fill_manual(values=c("gray50", "lightblue", "black", "blue"))+
geom_bar(stat = "identity", color="white") +
facet_grid(.~residence)
Finally, consider using facet_wrap()
. This can allow you to stack as you wanted, but you will not have the age comparison side by side
ggplot(df,
aes(x=agree,
y=round(percent, digits = 2),
fill = interaction(sex, age))) +
scale_fill_manual(values=c("gray50", "lightblue", "black", "blue"))+
geom_bar(stat = "identity", color="white") +
facet_grid(age~residence)
注目すべきことに、パーセントを2桁に丸めると、それは消えます(値は0.0に丸められます)。アップロードした画像が実際に丸められているのか、データにゼロが追加されているのかわからない。これが最後のプロットですround(...)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加