0

I am trying to integrate multiple plot for file bar, where each file has two stack bar plot, and plot easily confused without wrap them into one single grid. However, I intend to improve the result of this plot that add common legend and label for whole graph. I tried several away to integrate multiple plot for each file in more clear way, so putting these into one grid for file bar could be more elegant and easy to understand the output. I confused about the answer from several similar post in SO, bit of new with ggplot2, I couldn't produce my desired plot at the end. Can any give me possible idea to improve this current plot in better way ? How can I add common label and legend for multiple graph ? Any idea please ?

reproducible data.frame :

Qualified <- list(
    hotan = data.frame( begin=c(7,13,19,25,31,37,43,49,55,67,79,103,31,49,55,67), 
                        end=  c(10,16,22,28,34,40,46,52,58,70,82,106,34,52,58,70), 
                        pos.score=c(11,19,8,2,6,14,25,10,23,28,15,17,6,10,23,28)),
    aksu = data.frame( begin=c(12,21,30,39,48,57,66,84,111,30,48,66,84), 
                       end=  c(15,24,33,42,51,60,69,87,114,33,51,69,87), 
                       pos.score=c(5,11,15,23,9,13,2,10,16,15,9,2,10)),
    korla = data.frame( begin=c(6,14,22,30,38,46,54,62,70,78,6,30,46,70), 
                        end=c(11,19,27,35,43,51,59,67,75,83,11,35,51,75), 
                        pos.score=c(9,16,12,3,20,7,11,13,14,17,9,3,7,14))
)

unQualified <- list(
    hotan = data.frame( begin=c(21,33,57,69,81,117,129,177,225,249,333,345,33,81,333), 
                        end=  c(26,38,62,74,86,122,134,182,230,254,338,350,38,86,338), 
                        pos.score=c(7,34,29,14,23,20,11,30,19,17,6,4,34,23,6)),
    aksu = data.frame( begin=c(13,23,33,43,53,63,73,93,113,123,143,153,183,33,63,143), 
                       end=  c(19,29,39,49,59,69,79,99,119,129,149,159,189,39,69,149), 
                       pos.score=c(5,13,32,28,9,11,22,12,23,3,6,8,16,32,11,6)),
    korla = data.frame( begin=c(23,34,45,56,67,78,89,122,133,144,166,188,56,89,144), 
                        end=c(31,42,53,64,75,86,97,130,141,152,174,196,64,97,152), 
                        pos.score=c(3,10,19,17,21,8,18,14,4,9,12,22,17,18,9))
)

I am categorzing data and get multiple plot in this way (mainly influenced by @Jake Kaupp's idea) :

multi_plot <- function(x) {
    p1 <- ggplot(x, aes(x = group)) +
        geom_bar(aes(fill = elm), color = "black")
    p2 <- ggplot(distinct(x), aes(x = elm)) +
        geom_bar(aes(fill = group), color = "black")
    arrangeGrob(p1, p2,nrow = 1, top = unique(x$list))
}

singleDF <- 
    bind_rows(c(Qualified = Qualified, Unqualified = unQualified), .id = "id") %>% 
    tidyr::separate(id, c("group", "list")) %>%
    mutate(elm = ifelse(pos.score >= 10, "valid", "invalid")) %>% 
    arrange(list, group, desc(elm))

plot_data <- singleDF %>% 
    split(.$list) %>% 
    map(~split_plot(.x))
grid.arrange(grobs = plot_data, nrow = 1)

I am trying to integrate multiple plot for file bar with common label and common legend position. In terms of common legend, I intend to call X axis as sample, Y axis as observation; in terms of common legend position, I intend to indicate legend at right side of plot (only four common legend).

EDIT:

In my desired output plot, stack bar plot of group and elm must be put in one single grid for file bar. Regarding whole graph, pursuing common label and legend is desired.

How can I achieve my desired output ? What change has to be taken in original implementation ? sorry for this simple question in SO. Thanks in advance

Jerry07
  • 929
  • 1
  • 10
  • 28
  • "one single grid for file bar" -> I don't get what you mean by 'file' here. – Axeman Jan 03 '17 at 12:34
  • @Axeman I continued this post based on idea of yours and `Jake Kaupp `, in previous post, you presented the solution of generating multiple plot based on the condition. Is this solution consistent with previous one ? Thanks for your favor :) – Jerry07 Jan 03 '17 at 13:02
  • 1
    I proposed two seperate plots, since you are essentially plotting two different variables on the x-axis. I don't like to do that in a single plot. I think it is confusing. But since you seem to insist, I now provided a way to plot everything into a single plot. Yes, the results are the same afaik. – Axeman Jan 03 '17 at 13:05

1 Answers1

3
combinedDF <- 
  bind_rows(mutate(singleDF, x = group, fill = elm),
            mutate(singleDF, x = elm, fill = group) %>% distinct()) %>% 
  mutate(x = factor(x, levels = c('invalid', 'valid', 'Unqualified', 'Qualified')),
         fill = factor(fill, levels = c('invalid', 'valid', 'Unqualified', 'Qualified')))

ggplot(combinedDF, aes(x = x, fill = fill)) +
  geom_bar() +
  geom_text(aes(label = ..count..), stat = 'count', position = 'stack') +
  facet_grid(~list)

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • why not we use multiple plot solution in your previous idea? – Jerry07 Jan 03 '17 at 13:05
  • Could I get bit more interpretation for your factorization part, `line-4`, `line-5`? Thanks – Jerry07 Jan 03 '17 at 13:12
  • 1
    That's just to reorder the x-axis and colors, so you don't get the alphabetical order (since that doesn't make much sense here). [See also here](http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph). – Axeman Jan 03 '17 at 13:14
  • sorry for this simple question: how can I make `line-4`, `line-5` more prettier ? I mean avoid that repeated character string ? Is that possible ? – Jerry07 Jan 03 '17 at 13:19
  • 3
    Use `levels = l` and define `l` beforehand. Or replace line 5 with `fill = factor(fill, levels = levels(x))` – Axeman Jan 03 '17 at 13:22
  • Dear Axeman, many thanks for your helpful instruction and answer, appreciated :) – Jerry07 Jan 03 '17 at 13:27