Search code examples
rdataframeggplot2plothistogram

Histograms with different bins grouped


I have this dataset:

Set,SVMLinear,SVMPoly
    x,0.5,0.6
    y,0.9,0.8
    z,0.8,0.7

How can I compute a ggplot in R such that in the x-axis are presented the 3 Sets (x, y, z), and for each set I have 2 bins (one for SVMLinear and the other for SVMPoly) with 2 different colors?

Thanks


Solution

  • Sounds like you're after a bar chart? Here's a solution using ggplot2 and data.table:

    library(ggplot2)
    library(data.table)
    dt <- data.table(
      Set=c("x","y","z"),
      SVMLinear=c(0.5,0.9,0.8),
      SVMPoly=c(0.6,0.8,0.7)
    )
    dt <- melt(dt, id.vars="Set")
    
    ggplot(data=dt, aes(x=Set, y=value, fill=variable)) +
      geom_col(position = "dodge") +
      scale_fill_discrete(name="Model:")
    

    The chart looks like this:

    enter image description here