JK Lambert
JK Lambert

Reputation: 353

Adding a table next to a diverging stacked chart

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()

Upvotes: 1

Views: 65

Answers (0)

Related Questions