如何按条形图融合R data.frame和plot group

add*_*ons 4 r ggplot2 melt

我有以下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
...
Run Code Online (Sandbox Code Playgroud)

我的目标是通过条形图绘制一组(http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/section-包含更多变量的图表),如链接所示.

我意识到在开始之前我需要将数据转换为以下格式

  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
...
Run Code Online (Sandbox Code Playgroud)

我使用了熔化功能:

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

我不认为我正在融化正确,因为在我执行上面的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
Run Code Online (Sandbox Code Playgroud)

rns*_*nso 15

尝试:

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

要么

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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述