Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to melt R data.frame and plot group by bar plot

Tags:

r

ggplot2

melt

I have following 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
...

My goal is to plot a group by bar plot (http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/ section-Graphs with more variables) as shown in the link.

I realize that before getting to that I need to get the data in to following format

  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
...

I used the melt function:

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

I don't think I am doing the melt correctly because after I execute above groups.df.melt has 1000+ lines which doesn't make sense to me.

I looked at how Draw histograms per row over multiple columns in R and tried to follow the same yet I don't get the graph I want.

In addition I get following error: When I try to do the plotting:

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
like image 523
add-semi-colons Avatar asked Oct 13 '14 17:10

add-semi-colons


People also ask

How do you melt a data frame in R?

Melting in R It is performed using melt() function which takes dataset and column values that has to be kept constant. Using melt(), dataframe is converted into long format and stretches the data frame.

How do I make a grouped bar chart in R?

Method 2: Creating A Grouped Barplot Using ggplot2 Package Then the user needs to call the geom_bar() function from the ggplot package with the required parameters into it to create the grouped bar plot in the R programming language.

How do you plot a bar graph in R?

Bar plots can be created in R using the barplot() function. We can supply a vector or matrix to this function. If we supply a vector, the plot will have bars with their heights equal to the elements in the vector.

Can I plot Dataframe in R?

The plot() function is defined as a generic function for plotting in R Language. It can be used to create basic plots of a different type. In the code below first dataframe column is X-axis, and the rest columns are y-axis, and they are plotted against the first column in form of a line chart.


1 Answers

Try:

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

or

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')

enter image description here

like image 111
rnso Avatar answered Nov 15 '22 07:11

rnso