How to control space between stack bars in ggplot2?

ぃ、小莉子 提交于 2019-12-05 10:36:05

The "duplicate question" comments above will lead you to an answer like this one:

library(dplyr)
library(ggplot2)
dummydf <- expand.grid(var1 = paste0("trt", 1:5),
            var3 = paste0("C_", 11:15)) %>% 
  mutate(value = runif(length(var1)),
         var2 = ifelse(var1 %in% c("trt1", "trt2"), "grp1", "grp2"))


  ggplot(dummydf, aes(var1, value, fill = var3)) +
  geom_col(position = "stack") +
  facet_grid(~var2, scales = "free_x", space = "free_x") 

And this solution is sometimes great! The advantages are:

  1. it's simple to implement
  2. contains the labels for the hierarchical grouping at the top
  3. generally looks nice
  4. is easily customizable.

For example:

  ggplot(dummydf, aes(var1, value, fill = var3)) +
  geom_col(position = "stack") +
  facet_grid(~var2, scales = "free_x", space = "free_x") +
  theme(panel.spacing = unit(3, "cm"),
        strip.text = element_text(size = 12, family = "mono"))

The main disadvantages to this method:

  1. If this is already part of a facet panelled plot, it makes the whole thing cluttered.
  2. If your hierarchical groups are obvious from the treatments, you may not need explicit labelling, just a quick visual distinction. For example, say the groups are control/intervention, and your treatments are "no drug, placebo" and "drugs 1, 2, and 3".

So here's an alternate method:

dummydf %>% 
  bind_rows(data_frame(var1 = "trt99")) %>% 
  ggplot(aes(var1, value, fill = var3)) +
  geom_col(position = "stack") +
  scale_x_discrete(limits = c("trt1", "trt2", "trt99", "trt3", "trt4", "trt5"),
                   breaks = c("trt1", "trt2",      NA, "trt3", "trt4", "trt5"),
                   labels = c("trt1", "trt2",      "", "trt3", "trt4", "trt5"))

This solution has its own drawbacks, primarily that you can only customize the space in a limited way. You can create a "false" bar equal to an integer multiple of the widths of the bars you've already got by adding additional false levels to your limits, breaks, and labels. But you can't create a space that's only half a bar wide.

You could provide additional information in the false bar space though:

  1. Adding a text annotation in the plot area
  2. Replacing the NA and "" in breaks and labels with trt99 and "<-group1 | group2->" or something similar.

I think you just need to create a new column to your data set which labels everything is "treatment3" and "not treatment3". I used dplyr package:

df1 = dplyr::mutate(mydata, 
    var4 = ifelse(var1 == "treatment3", "treatment3", "not treatmeant3"))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!