在ggplot中的组之间添加空行/空格

时间:2017-12-05 16:36:00

标签: r ggplot2

我想在组之间添加空行以使图形看起来不那么密集。 前10行数据附在此处:

> dput(droplevels(head(hosp, 10)))
structure(list(mzipid = c("FL-3", "FL-3", "FL-3", "FL-4", "FL-4", 
"FL-4", "FL-4", "FL-4", "FL-4", "FL-4"), region_oe = c(1.6459234, 
1.6459234, 1.6459234, 1.3399296, 1.3399296, 1.3399296, 1.3399296, 
1.3399296, 1.3399296, 1.3399296), region_low_ci = c(0.53041852, 
0.53041852, 0.53041852, 0.9696098, 0.9696098, 0.9696098, 0.9696098, 
0.9696098, 0.9696098, 0.9696098), region_high_ci = c(3.8410261, 
3.8410261, 3.8410261, 1.8049299, 1.8049299, 1.8049299, 1.8049299, 
1.8049299, 1.8049299, 1.8049299), hosp_id = structure(c(5L, 9L, 
4L, 7L, 8L, 6L, 2L, 1L, 9L, 3L), .Label = c("100025", "100054", 
"100093", "100106", "100113", "100122", "100124", "100223", "non_local_hosp"
), class = "factor"), outc_n = c(2L, 2L, 1L, 1L, 6L, 3L, 1L, 
19L, 2L, 9L), pre_outc_n = c(0.339173126965761, 0.577540323603898, 
0.838447939604521, 0.463049655780196, 2.99622658733279, 1.86155441217125, 
0.626489417627454, 12.4346171403304, 1.37301584333181, 6.90619195345789
), oe = c(NA, NA, NA, NA, 2.0025189, 1.6115564, NA, 1.5279924, 
NA, 1.3031784), low_ci = c(NA, NA, NA, NA, 0.73123336, 0.32389662, 
NA, 0.91951913, NA, 0.59465784), high_ci = c(NA, NA, NA, NA, 
4.3587809, 4.7086916, NA, 2.3862863, NA, 2.4740036), state = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "FL", class = "factor"), 
    id = 1:10, outlier = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L), .Label = "black", class = "factor"), cv = c(" 66.88", 
    " 66.88", " 66.88", " 38.37", " 38.37", " 38.37", " 38.37", 
    " 38.37", " 38.37", " 38.37"), start_id = c(1L, 1L, 1L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L), end_id = c(3L, 3L, 3L, 12L, 12L, 
    12L, 12L, 12L, 12L, 12L)), .Names = c("mzipid", "region_oe", 
"region_low_ci", "region_high_ci", "hosp_id", "outc_n", "pre_outc_n", 
"oe", "low_ci", "high_ci", "state", "id", "outlier", "cv", "start_id", 
"end_id"), row.names = 105:114, class = "data.frame")

目前的代码是这样的:

## draw graph
ggplot(hosp, aes(x = id, y = oe, group = mzipid)) +
  theme(panel.background = element_rect(fill = "white", colour = "grey50")) +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  geom_point(size=1, show.legend = FALSE, colour = hosp$outlier) +
  geom_linerange(aes(ymin = low_ci, ymax = high_ci), alpha = 0.4, na.rm = TRUE, colour = hosp$outlier)) +
  scale_linetype_manual(values = linetype, guide = FALSE) +
  geom_segment(aes(x = start_id, xend = end_id, y = region_oe, yend = region_oe, linetype = "1",
                   size = 1), na.rm = TRUE) + 
  geom_ribbon(aes(ymin = region_low_ci, ymax = region_high_ci), alpha=0.2, linetype = "blank", show.legend = FALSE) +
  geom_hline(aes(yintercept = 1, alpha = 0.2, colour = "red", size = 1), show.legend = FALSE) +
  scale_size_identity() +
  scale_x_continuous(name = "hospital id", breaks = seq(0,350, by = 20)) +
  scale_y_continuous(name = "O:E ratio", breaks = seq(0,6, by = 0.5), 
                     sec.axis = sec_axis(trans = ~., breaks = c(0.1), 
                                         labels = "coefficient of variation")) +
  stat_summaryh(fun.x = mean, aes(label = mzipid, y = 5.3), geom = "text", size = 2, srt = 35, 
                check_overlap = F) +
  stat_summaryh(fun.x = mean, aes(label = cv, y = 0.1), geom = "text", size = 2, srt = 35,
                check_overlap = F) +
  guides(colour = FALSE) +
  ggtitle("O:E ratio:hospital level\nmedian clusters") + 
  theme(plot.title = element_text(hjust = 0.5)) 

目前的图表如下所示: enter image description here

如何在每个zip组(mzipid)之间添加空行/空格?鉴于提供的数据,如果任何人都可以展示如何在FL-3组和FL-4组之间添加空间将会很棒。

谢谢!

0 个答案:

没有答案