如何减少分组的堆叠条形图之间的空白

时间:2020-06-08 10:37:02

标签: r

我正在尝试创建分组的堆栈条形图。我快到了(请在下面查看一个可重现的小示例)。

下面的测试数据集

structure(list(id = c("bmi", "bp.syst", "cholesterol", "current_smoker", 
"dm", "bmi", "bp.syst", "cholesterol", "current_smoker", "dm", 
"bmi", "bp.syst", "cholesterol", "current_smoker", "dm", "bmi", 
"bp.syst", "cholesterol", "current_smoker", "dm"), age_cat = c("50-59", 
"50-59", "50-59", "50-59", "50-59", "50-59", "50-59", "50-59", 
"50-59", "50-59", "50-59", "50-59", "50-59", "50-59", "50-59", 
"50-59", "50-59", "50-59", "50-59", "50-59"), sex = c("female", 
"female", "female", "female", "female", "female", "female", "female", 
"female", "female", "male", "male", "male", "male", "male", "male", 
"male", "male", "male", "male"), outcome = c("is", "is", "is", 
"is", "is", "mi", "mi", "mi", "mi", "mi", "is", "is", "is", "is", 
"is", "mi", "mi", "mi", "mi", "mi"), ce = c(-0.10256038920815, 
0.389531808498818, 0.189757379029111, 0.242653828895447, -0.00912095569278522, 
-0.0886249777508804, 0.30613965729692, 0.360665863387202, 0.243896737961612, 
-0.0510720373492212, -0.0872536577940028, 0.445136499987966, 
0.285697713680353, 0.173800331570914, -0.0165390351091511, -0.0753370972092315, 
0.353576764190506, 0.506875636047312, 0.174734750863782, -0.0874725134917425
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-20L))

下面的ggplot代码

ggplot() + 
  geom_bar(data=test, aes(x = ce, y=outcome, fill=id), position="stack", stat="identity",width=0.25)+
  geom_vline(xintercept = 0, color =c("white")) +
  scale_fill_viridis(option = "plasma",
                 discrete =T,
                 name = 'Risk factor',
                 labels = c('Body mass index', 'Systolic blood pressure', 'Cholesterol', 'Smoking',
'Diabetes'))+
  ggtitle("Potential impact fraction by condition and sex") +
  theme_ipsum() +
  theme(legend.position="right") +
  xlab('Percentage')+
  facet_grid(sex~1)

我想显着减少性别类别中的空白,但仍然在性别类别之间保留一些空白

谢谢

2 个答案:

答案 0 :(得分:2)

您可以使用width中的geom_bar参数来减小小节之间的间距,而可以使用theme()panel.spacing.y中设置小平面之间的间距。为了获得其他参数来控制条的“宽度”,我添加了scale_y_discrete(expand = expansion(add = c(2, 2))),它扩展了y轴,从而“挤压”了条。

library(ggplot2)
library(hrbrthemes)
library(viridis)
#> Loading required package: viridisLite
ggplot() + 
  geom_bar(data=test, aes(x = ce, y=outcome, fill=id), position="stack", stat="identity", width = .6)+
  geom_vline(xintercept = 0, color =c("white")) +
  scale_y_discrete(expand = expansion(add = c(2, 2))) +
  scale_fill_viridis(option = "plasma",
                     discrete =T,
                     name = 'Risk factor',
                     labels = c('Body mass index', 'Systolic blood pressure', 'Cholesterol', 'Smoking',
                                'Diabetes'))+
  ggtitle("Potential impact fraction by condition and sex") +
  theme_ipsum() +
  theme(legend.position="right", panel.spacing.y = unit(1, "cm")) +
  xlab('Percentage')+
  facet_grid(sex~1)

reprex package(v0.3.0)于2020-06-08创建

答案 1 :(得分:1)

您可以结合使用gridgtable软件包中的功能,以可视化和更改不同对象之间的间距。

library(ggplot2)
library(viridis)    # for scale_fill_viridis
library(hrbrthemes) # for theme_ipsum
library(grid)       # for ggplot_build and ggplot_table
library(gtable)     # for gtable_show_layout

## create and save plot in object "plot_01"
plot_01 <- ggplot() + 
  geom_bar(data = test, aes(x = ce, y = outcome, fill = id), position = "stack", 
           stat = "identity", width = 0.25)+
  geom_vline(xintercept = 0, color = c("white")) +
  scale_fill_viridis(option = "plasma",
                     discrete = TRUE,
                     name = 'Risk factor',
                     labels = c('Body mass index', 'Systolic blood pressure', 
                                'Cholesterol', 'Smoking', Diabetes'))+
  ggtitle("Potential impact fraction by condition and sex") +
  theme_ipsum() +
  theme(legend.position = "right") +
  xlab('Percentage')+
  facet_grid(sex ~ 1)

这是您的原始图,现在保存到“ plot_01”

plot_01

enter image description here

# make an object, that is easier accessible to change
grid_01 <- ggplot_gtable(ggplot_build(plot_01))

# look at the layout
# a visualisation makes this a lot easier
gtable_show_layout(grid_01)

这给出了原始布局:

enter image description here

## make a copy to play around with
grid_02 <- grid_01
## increase the height between male/female times 4
grid_02$heights[9] <- grid_02$heights[9] * 4

如果比较grid_01和grid_02之间的高度,则会看到差异: enter image description here

## and look at the effect in the layout
gtable_show_layout(grid_02)

这将产生更改后的网格:

enter image description here

grid.newpage()      # needed to avoid plotting on top of existing plot
grid.draw(grid_02)  # plot your altered pic

这反过来为您提供了一个图形,该图形具有您所描述的更多间距。

enter image description here

当然,您可以在情节中与这个空间和其他空间一起玩耍。

请让我知道这是否是您想要的。