compare different datasets with stacked bar graphs in R

后端 未结 2 2024
眼角桃花
眼角桃花 2021-01-28 01:18

I need to compare two different methods that each of them has 3 different results in one graph with using stacked bar style.

I want to draw a plot so that x axis shows t

相关标签:
2条回答
  • 2021-01-28 02:14

    You said that you need to compare the methods. If you represent experiment on x-axis and result on y then how will you represent method??? My way of doing it is using the facet. Here is the code for how to do it using ggplot2.

    dat <- read.csv("data.csv")
    library(reshape2)
    library(ggplot2)
    dat1 <- melt(dat,id.vars = c("experiment","method"))
    p <- ggplot(dat1,aes(experiment,value,fill=variable))+geom_bar(stat="identity")+
          facet_wrap(~method,nrow=1)
    p
    

    Plot

    0 讨论(0)
  • 2021-01-28 02:18

    This sort of multi-dimensional chart is best explored using the ggplot2 package. I will assume here that the data you have pasted is stored in the data.frame d:

    require(reshape2)    ## needed to have all experiments in one variable
    require(ggplot2)     ## needed for the great vizualizations
    
    d <- melt(d, id.vars=c("experiment", "method"))
    ggplot(d, aes(x=factor(experiment), y=value, fill=variable)) + 
      geom_bar(stat="identity") + 
      facet_wrap(~method)
    

    You can polish the graph further using custom labels, but that is too long to explore here. The questions with the ggplot2 tag have lots of great examples.

    EDIT: Corrected to show the methods too, as already answered by @user2743244

    0 讨论(0)
提交回复
热议问题