Stacked bar chart with separate segments ?

Hello everyone...

...this is my first post in this community and I'm completely new to RStudio.

For my bachelor thesis I intend to use a stacked bar chart using ggplot2.
Researching the internet, I found an image with an interesting chart variant with separated segments which I find much easier to read.

Does anyone know if such a chart can be created with ggplot2 or other packages ?

Does anyone have experience with this kind of bar chart ?

Thanks for your help in advance.

Personally, I don't know a package that does something like this out of the box, but in principle, there is nothing out of extraordinary that I can see with the chart on the right.
Take a look here - https://ggplot2.tidyverse.org/reference/facet_grid.html.

1 Like

:+1: I think facetting is the right approach. It's basically a single row with faceted bar-chart columns that aren't labelled. The y-axis is categorical, and the x-axis appears to be value based on the unshown variable that represents the segments in the first chart.

If out-of-the-box facetting doesn't work, you might want to take a look at the cowplot package, perhaps plot_grid().

1 Like

Welcome to the community.

Are you new to R as well as RStudio?

This should get you started with your plot.

Ron.

library(tidyverse)

set.seed(12345)

# Dummy dataset. nb, I've created the numbers to be represented in the bars,
# the y values, and therefore use stat = 'identity' in geom_bar. Depending on
# your data you may not want to do that.
DF <- tibble(xstr = rep(LETTERS[1:5],each=4),
             yval = runif(20),
             what = rep(letters[1:4],times=5))

ggplot(DF) +
    geom_bar(aes(x = xstr,
                 y = yval,
                 fill = what), stat = 'identity') +
    facet_wrap(~ what, nrow = 1) +
    coord_flip() +
    theme_bw() + theme(strip.text = element_blank(),
                       axis.title = element_blank(),
                       axis.ticks = element_blank(),
                       axis.text.x = element_blank(),
                       panel.grid = element_blank(),
                       panel.border = element_blank())

Created on 2019-03-25 by the reprex package (v0.2.1)

3 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.