1

I would like to know if there is any way to generate an annotation next to the diverging stacked bar chart I have attached. this chart

Specifically, I want to show a table to the right. The table will have three rows and three columns. I would like to have each row of the table exactly aligned to each of the horizontal bars.

For example:
-----BAR 1------ cell1 | cell2 | cell3

-----BAR 2------ cell4 | cell5 | cell6

-----BAR 3------ cell7 | cell8 | cell9

The cells do not have to be from the data we used to construct the plot, it can be any number or string.

Can this be done by adding some table as annotation in ggplot? Is it possible to do it at all?

Btw. the code used to produce the chart is as follows:

dat <- read.table(text = "    ONE TWO THREE
                  1   23  234 324
                  2   34  534 12
                  3   56  324 124
                  4   34  234 124
                  5   123 534 654",sep = "",header = TRUE)
datm <- melt(cbind(dat, ind = rownames(dat)), id.vars = c('ind'))
ggplot(datm, 
       aes(x = variable, y = ifelse(ind %in% 1:2, -value, value), 
       fill = ind)) + 
    geom_col() +
    coord_flip()
stefan
  • 90,330
  • 6
  • 25
  • 51
JK Lambert
  • 333
  • 1
  • 6
  • Does this answer your question? https://stackoverflow.com/questions/12318120/adding-table-within-the-plotting-region-of-a-ggplot-in-r or https://stackoverflow.com/questions/60349028/how-to-add-a-table-to-a-ggplot or https://stackoverflow.com/questions/40335838/adding-a-table-to-ggplot-figure or https://stackoverflow.com/questions/34770930/adding-a-table-to-ggplot-with-gridextra-and-annotation-custom-changes-y-axis-l – Jon Spring May 18 '21 at 15:37
  • Almost, it is a good example, but I need it to the right of the chart, like every time you see a bar in the chart to the right there is some row of the table explaining "extra issues" of that very specific bar. I wonder if the previous example can be scaled down. But the bar plot type i am using is very important as well. – JK Lambert May 18 '21 at 15:41
  • Here's a blog post about doing that using the `patchwork` package: https://mattherman.info/blog/ppt-patchwork/ – Jon Spring May 18 '21 at 15:43
  • Almost! Very important part is that every row must match every bar so with your finger you can jump between the table and the image, – JK Lambert May 18 '21 at 15:45

0 Answers0