Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Stacked & Clustered Bar Graph using R

I want to visualise a Clustered & Stacked Bar Chart in Power BI but Apparently it is not possible. https://ideas.powerbi.com/forums/265200-power-bi-ideas/suggestions/6804910-clustered-stacked-chart ...
  • Icey's avatar
    7 years ago

    Hi Anonymous ,

    You need to change your data frame like below:

    Then, you can refer to the article to create a Stacked & Clustered Bar Graph using R.

    The code is below:

    >install.packages(“ggplot2”)

    >library(ggplot2)

    > data<-read.table(file="Budget.txt",header=T)

    > ggplot(data=data, aes(x=Month, y=Value,fill=Category)) + geom_bar(stat="identity")

    > data2<-with(data, data[order(Month, Category, Actual.Budget),])

    > ggplot(data=data2, aes(x=Actual.Budget, y=Value,fill=Category)) + geom_bar(stat="identity")+facet_grid(~Month)

     

    Best Regards,

    Icey Zhang

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

  • Icey's avatar
    7 years ago

    Hi Anonymous ,

    In Power BI, you can write your R script like so:

     

    library(ggplot2)
    data<-dataset
    data<-with(data,data[order(Month,Category,ActualBudget),])
    ggplot(data=data, aes(x=ActualBudget, y=Value,fill=Category)) + geom_bar(stat="identity")+facet_grid(~Month)

     

    This is my sample file.

     

    Best Regards,

    Icey

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.