Forum Discussion
Stacked bar chart sorting issues
- 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
- https://www.amazon.de/ggplot2-Elegant-Graphics-Data-Analysis/dp/331924275X/ref=sr_1_1?ie=UTF8&qid=1508936038&sr=8-1&keywords=ggplot2 (this is from the designer of the ggplot2 package, a great but not an easy read)
- https://www.amazon.de/R-Graphics-Cookbook-Winston-Chang/dp/1449316956/ref=sr_1_2?ie=UTF8&qid=1508936146&sr=8-2&keywords=ggplot2 (a good start)
Regards
Tom
Hi Tom,
worked out great!
Just one thing I am still not able to show, is the number auf days within the bars.
Any good tutorial on how to add this and maybe change the colors?
Many thanks.
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
- https://www.amazon.de/ggplot2-Elegant-Graphics-Data-Analysis/dp/331924275X/ref=sr_1_1?ie=UTF8&qid=1508936038&sr=8-1&keywords=ggplot2 (this is from the designer of the ggplot2 package, a great but not an easy read)
- https://www.amazon.de/R-Graphics-Cookbook-Winston-Chang/dp/1449316956/ref=sr_1_2?ie=UTF8&qid=1508936146&sr=8-2&keywords=ggplot2 (a good start)
Regards
Tom