15

I have the following graph

test  <- data.frame(person=c("A", "B", "C", "D", "E"), 
                    value1=c(100,150,120,80,150),     
                    value2=c(25,30,45,30,30) , 
                    value3=c(100,120,150,150,200)) 

I want to plot a grouped barchart (horizontal) for each person where one bar indicates value1 and the other bar is stack of value2 and value3. Is there a way with which I can do this using ggplot2? Can I use facets to plot these individual graphs one below the other?

Victor K.
  • 4,054
  • 3
  • 25
  • 38
Sudhi
  • 185
  • 1
  • 1
  • 6
  • 6
    Welcome to SO. Please share what you have tried so far and how it hasn't worked. This isn't a place to go and ask us to generate code for you. Instead, try a few things and formulate a question about your code and how it is or isn't working. – Justin Sep 12 '13 at 21:29
  • 2
    Downvote comment: Little research effort. – IRTFM Sep 12 '13 at 22:57
  • 1
    @SeñorO - you realize I was **NOT** the author of the original question, don't you? I've just edited it – Victor K. Sep 13 '13 at 17:42
  • 1
    This question and answer are far more relevant to my problem than the Q&A that this question purportedly duplicates. Moreover, I think the explanation by @VictorK is easier to understand. – Dan Barowy Aug 25 '17 at 15:37

1 Answers1

45

Here is what I came up with, similar to a solution proposed here: stacked bars within grouped bar chart

  1. Melt data.frame and add a new column cat

    library(reshape2) # for melt
    
    melted <- melt(test, "person")
    
    melted$cat <- ''
    melted[melted$variable == 'value1',]$cat <- "first"
    melted[melted$variable != 'value1',]$cat <- "second"
    
  2. Plot a stacked chart cat vs value, faceting by person. You may need to adjust the labels to get what you want:

    ggplot(melted, aes(x = cat, y = value, fill = variable)) + 
      geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ person)
    

enter image description here

Community
  • 1
  • 1
Victor K.
  • 4,054
  • 3
  • 25
  • 38
  • This is a great plot! Is there a way to alter it so that value 2, for example, can also be stacked onto the "first" bar chart as well? I have a similar plot that has "values" that are included in both "first" and "second", and I do not know how to add it to the plot without making a redundant label on the legend – Captain Murphy Aug 05 '14 at 21:56
  • Gotta love how easy ggplot makes it to produce pretty graphs. I covered creating stacked / grouped bar charts [here](http://theduke.at/blog/science/beginners-guide-to-creating-grouped-and-stacked-bar-charts-in-r-with-ggplot2/). – theduke Sep 09 '16 at 11:54
  • 1
    [geom_col()](https://ggplot2.tidyverse.org/reference/geom_bar.html) also makes a plot where the height of the bars represent values in the data. With `geom_col` there is no need to specify `stat = 'identity'`. To generate the plot above: `ggplot(melted, aes(x = cat, y = value, fill = variable)) + geom_col() + facet_grid(~ person)`. – Paul Rougieux Aug 27 '18 at 14:35
  • 1
    But how to make the same but with with same labels/var for different clusters. Meaning you have `(first - (var1,var2), second - (var1,var2))` for multiple clusters? – denis631 Oct 21 '18 at 20:03