How to produce stacked bars within grouped barchart in R [duplicate]

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

Leave a Comment

tech