离散比例

时间:2018-03-22 11:54:46

标签: r ggplot2

我使用ggplot2创建了一个多面森林图。 y标度使用离散因子。对于面板背景,我希望有交替的矩形(灰色,白色),它跨越轴刻度的两侧50%和y轴上的面板网格线,以改善相邻因子点之间的背景对比度。从其他一些关于SO的问题我相信geom_rect是我应该使用的但是我无法弄清楚如何使用离散因子标度来交替使用矩形。提前谢谢。

示例图:

require(tidyverse)

##data frame
df1 <- tibble('outcome.var' = c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 
'J'),
          'coefficient' = c(-0.08, -0.01, -0.06, 0.02, 0.01, 0.02, -0.08, -0.1, 0.1, 0.2),
          'conf.low' = c(-0.12, -0.03, -0.09, 0.01, 0.02,  0.01, -0.10, -0.2, 0.05, 0.1),
          'conf.high' = c(-0.05, 0.02, -0.03, 0.03, -0.01, 0.04, -0.06, 0, 0.2, 0.3),
          'p.value' =c(0.01, 0.02, 0.05, 0.10, 0.02, 0.13, 0.11, 0.01, 0.01, 0.04)) %>%
  mutate(significant = ifelse(p.value > 0.05, 'P > 0.05', 'P < 0.05'),
     label = case_when(
       .$outcome.var %in% c('A', 'B', 'C') ~ '1',
       .$outcome.var %in% c('D', 'E', 'F') ~ '2',
       .$outcome.var %in% c('G', 'H') ~ '3',
       .$outcome.var %in% c('I', 'J') ~ '4'))


p1 <- ggplot(data = df1, aes(outcome.var, coefficient)) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high), shape = 32) +
  geom_point(aes(shape = significant), fill = 'white') +
  geom_hline(mapping = NULL, data = NULL, yintercept = 0, colour = "grey40", 
      size = 0.5, linetype = "solid") +
  theme(panel.background = element_rect(fill = "grey95")) +
  scale_shape_manual(values = c(19, 21)) +
  scale_colour_manual(values = c('black', 'black')) + 
  theme(panel.spacing = unit(1, "lines")) +
  theme(legend.title = element_blank()) +
  xlab(NULL) +
  coord_flip() +
  facet_grid(label ~ ., scales = "free", space = "free", switch = "x") +
  theme(strip.text.y = element_blank()) +
  theme(legend.text = element_text(face = 'bold'))

0 个答案:

没有答案