2017-04-18 7 views
1

私のコードはここに問題ggplot2と棒グラフfacet_grid機能を使用して

library(ggplot2) 

fig_num <- ggplot(data=branch.fig, aes(y=branch.fig$num, x=branch.fig$film.type, fill = branch.fig$film.type)) + 
    geom_bar(position=position_dodge(), stat="identity", width = 0.3, colour = "gray35") + 
    scale_fill_manual(values = colors) + 
    facet_grid(branch.type~.) + 
    geom_errorbar(aes(ymin=branch.fig$num, ymax=branch.fig$num +branch.fig$SE), position="dodge", width=0.1, col = "gray40") + 
    theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) + 
    labs(x = "Film Type",y = "Branch Number") + 
    ggtitle("DAS = 54") + coord_flip() 
fig_num 

である私が持っているしたい結果が

enter image description here

、このようにする必要がありますしかし、私が得るのはこれのようなものです。データのRの部分を組み合わせると、なぜこれが現れるのか分かりません。本当に大きな問題であり、本当に助けを探しています。ありがとうございます!

enter image description here

次のコードを持つ多くの問題があるデータセット

dput(subset(branch.fig)) 
structure(list(film.type = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L), .Label = c("black", "ck", "dark-gray", 
"green", "red", "white"), class = "factor"), branch.type = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("F", 
"V"), class = "factor"), num = c(14.6666666666667, 3.33333333333333, 
14, 2.66666666666667, 15.3333333333333, 2, 14.6666666666667, 
2.66666666666667, 16.6666666666667, 3.33333333333333, 20.3333333333333, 
1.66666666666667), SE = c(0.333333333333333, 0.333333333333333, 
1.15470053837925, 0.333333333333333, 0.881917103688197, 0, 0.881917103688197, 
0.666666666666667, 0.333333333333333, 0.666666666666667, 2.33333333333333, 
0.666666666666667), n = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L)), .Names = c("film.type", "branch.type", "num", "SE", 
"n"), row.names = c(NA, -12L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = list(film.type), drop = TRUE, indices = list(
    0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L, 
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
film.type = structure(1:6, .Label = c("black", "ck", "dark-gray", 
    "green", "red", "white"), class = "factor")), row.names = c(NA, 
-6L), class = "data.frame", vars = list(film.type), drop = TRUE, .Names = 
"film.type")) 
+1

[再現可能な形式](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)にデータを入力してください。自分のデータのスクリーンショットをリンクしないでください。他の人が問題を解決する前にそれらを書き換える必要があります。 –

+0

私は質問を編集してデータセットをコピーしました〜 –

+0

残念なことに、 'dply'と' dput'が完全にうまくいっていないので、 'dput'出力が正しく動作しません。 'dput(ungroup(branch.fig)) 'の出力を投稿してみてください。(この質問のhttp://stackoverflow.com/questions/29039553/reproducible-example-and-dput-error) – bdemarest

答えて

1

です。
1. dputがエラーを生成しました。以下のコマンドはデータを生成します。
2.データフレームの名前はdata=branch.figです。 aes()の一部として再度名前を付ける必要はありません。
3.このチャートには泥沼がありませんので、dodgeコマンドをgeom_bargeom_errorbarの両方にドロップしてください。
4.私が見る限り、colorsオブジェクトはありません。私はマニュアルカラースケールのためにいくつかの色を入れました。

# Data 
branch.fig = structure(list(film.type = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L), .Label = c("black", "ck", "dark-gray", 
"green", "red", "white"), class = "factor"), branch.type = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("F", 
"V"), class = "factor"), num = c(14.6666666666667, 3.33333333333333, 
14, 2.66666666666667, 15.3333333333333, 2, 14.6666666666667, 
2.66666666666667, 16.6666666666667, 3.33333333333333, 20.3333333333333, 
1.66666666666667), SE = c(0.333333333333333, 0.333333333333333, 
1.15470053837925, 0.333333333333333, 0.881917103688197, 0, 0.881917103688197, 
0.666666666666667, 0.333333333333333, 0.666666666666667, 2.33333333333333, 
0.666666666666667), n = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L)), .Names = c("film.type", "branch.type", "num", "SE", 
"n"), row.names = c(NA, -12L), class = "data.frame") 


library(ggplot2) 

# Plot 
fig_num = ggplot(data = branch.fig, aes(y = num, x = film.type, fill = film.type)) + 
    geom_bar(stat = "identity", width = 0.3, colour = "gray35") + 
    scale_fill_manual(values = c("black", "orange", "grey", "green", "red", "white")) + 
    facet_grid(branch.type ~ .) + 
    geom_errorbar(aes(ymin = num, ymax = num + SE), width = 0.1, col = "gray40") + 
    theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) + 
    labs(x = "Film Type", y = "Branch Number") + 
    ggtitle("DAS = 54") + coord_flip() 

fig_num 
+0

お返事ありがとうございます!!!!問題は多分私のコードでデータフレームを繰り返したので、数字がaes()〜 –

関連する問題