`space =“ free”`不能消除ggplot方面的死空间

时间:2019-01-19 19:48:36

标签: r ggplot2

library(tidyverse)
library(scales)
dat <- read.table(text = "A B C
                          1   23  234 324
                          2   34  534 12
                          3   56  324 124
                          4   34  234 124
                          5   123 534 654",
                  sep = "", 
                  header = TRUE) %>% 
  gather(key = "variable", value = "value") %>% 
  mutate(ind = as.factor(rep(1:5, 3)))

ggplot(dat, aes(variable, value, fill = ind)) + 
  geom_bar(position = "fill", stat = "identity") +
  scale_y_continuous(labels = scales::percent_format()) + 
  facet_grid(~variable, space = "free") + 
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

我希望space = "free"中的facet_grid()参数可以消除下面每个方面的空白。例如,构面“ A”不应显示空格为“ B”和空格为“ C”的空格。

如何在所有三个方面消除此死区?我只希望每个构面显示一列,并且构面中的其他两个空白列不应被伪绘制。

spacefree

1 个答案:

答案 0 :(得分:2)

我相信,相反,您只想像scales = "free"那样facet_grid(~variable, scales = "free")就给

enter image description here

我相信是因为:

  

space-如果默认设置为“ fixed”,则所有面板的大小均相同。如果   “ free_y”的高度将与y的长度成比例   规模;如果“ free_x”,则其宽度将与   x标度或者,如果“免费”,则高度和宽度都会变化。 此   除非适当的比例也有所变化,否则设置无效。

因此,scales = "free"对于space产生作用是必不可少的,但是仅凭秤,我们已经实现了这种情况下需要的功能。

相关问题