Forum Discussion

BBCuser's avatar
BBCuser
New Member
8 years ago
Solved

Stacked bar chart sorting issues

Hi all,   I have an issue with a particular stacked bar chart. Even though I've read several post in regards to this issue I could not solve my problem.   Background: I have several people switch...
  • TomMartens's avatar
    TomMartens
    8 years ago

    Hey, great that it was helpful!

     

    I tweaked the code a little, redownload the pbix file and have another look. Now the chart looks like this

     

     

    You will find this little line inside the  R script

    manualcolors <- c("Loc A" = "#800000", "Loc B" = "blue", "Loc C" = "darkgreen")

    Here you can define the colors, the vector "manualcolors" is used further down in the script. Be aware that you can use hexcodes and also constant color names (be aware that you may know more colors by name than ggplot does :-)). I always recommend using hexcodes. Creating a dynamic number of  sequential or diverging colors depending on the distinct number of locations in your dataset, is more advanced.

     

    I also use the "geom_text" twice, to place the values inside the segments and also on top (outside) of the bar :-)

     

    The function aggregate (used to create the dataset for total values) seems to be a little outdated in comparison to the packages from the "tidyverse" (ggplot2 belongs to this group of packages) or in comparison to the usage of the data.table package (from my personal point of view, the fastest data munging, agregating, and other data related operations package available, but also not the easiest one).

     

    Asking for a good tutorial, maybe this will gets you started:

    https://www.datacamp.com/courses/data-visualization-with-ggplot2-1

     

    These books are good reads about ggplot2

    Regards

    Tom