5

私は次のR data.frameを持っています:

  group match unmatch unmatch_active match_active
1   A    10       4              0            0
2   B   116      20              0            3
3   c   160      27              1            4
4   D    79      17              0            3
5   E   309      84              4           14
6   F   643     244             10           23
...

私の目標は、リンクに示されているように、棒グラフ ( http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2) / section-Graphs with more variables ) ごとにグループをプロットすることです。

それに到達する前に、データを次の形式にする必要があることに気付きました

  group variable value
1   A    match    10
2   B    match   116
3   C    match   160
4   D    match    79
5   E    match   309
6   F    match   643
7   A    unmatch   4
8   B    unmatch  20
...

私は溶融機能を使用しました:

groups.df.melt <- melt(groups.df[,c('group','match','unmatch', 'unmatch_active', 'match_active')],id.vars = 1)

上記を実行した後、 groups.df.melt に1000行以上の行があり、意味がありません。

R で複数の列にわたって行ごとにヒストグラムを描画する方法を調べ、同じことを試みましたが、必要なグラフが得られません。

さらに、次のエラーが表示されます: プロットを実行しようとすると:

ggplot(groups.df.melt, aes(x='group', y=value)) + geom_bar(aes(fill = variable), position="dodge") + scale_y_log10()

Mapping a variable to y and also using stat="bin".
  With stat="bin", it will attempt to set the y value to the count of cases in each group.
  This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
  If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
  If you want y to represent values in the data, use stat="identity".
  See ?geom_bar for examples. (Deprecated; last used in version 0.9.2)
Error in pmin(y, 0) : object 'y' not found
4

1 に答える 1

18

試す:

mm <- melt(ddf, id='group')
ggplot(data = mm, aes(x = group, y = value, fill = variable)) + 
       geom_bar(stat = 'identity', position = 'dodge')

また

ggplot(data = mm, aes(x = group, y = value, fill = variable)) + 
       # `geom_col()` uses `stat_identity()`: it leaves the data as is.
       geom_col(position = 'dodge')

ここに画像の説明を入力

于 2014-10-13T17:27:57.760 に答える