在多面散点图中使用geom_blank扩展比例

时间:2019-06-15 17:52:10

标签: r ggplot2

我有以下情节:

plot

我的问题是,正如您所看到的,Y轴与数据之间绝对没有空间,而在另一侧,在数据与侧面之间绝对没有空间。
我想添加一些空间,仅在那儿。

[Edit] [但是,按空间表示,我的意思是扩大比例,以便我的绘图基本上从较早的日期开始并在较晚的日期结束。例如。如果数据从7月26日开始到12月24日结束,我希望该图可以覆盖7月20日至12月31日。]

我曾考虑过使用 geom_blank(),但是我真的不知道如何使它工作。 您可能还需要了解的一件事是,我的x轴为POSIXct格式。

这是我到目前为止用来获得该图的代码:

my.breaks <- as.POSIXct(c("2015-05-01", "2015-05-10", "2015-05-15", "2015-05-20", "2015-05-25",
             "2015-06-05", "2015-06-10", "2015-06-15", "2015-06-20", "2015-06-25",
             "2015-07-05", "2015-07-10", "2015-07-15", "2015-07-20", "2015-07-25",
             "2015-08-05", "2015-08-10", "2015-08-15", "2015-08-20", "2015-08-25",
             "2015-09-05", "2015-09-10", "2015-09-15", "2015-09-20", "2015-09-25",
             "2015-10-05", "2015-10-10", "2015-10-15", "2015-10-20", "2015-10-25",
             "2015-11-05", "2015-11-10", "2015-11-15", "2015-11-20", "2015-11-25",
             "2015-12-05", "2015-12-10", "2015-12-15", "2015-12-20", "2015-12-31"))

test.dat %>%
  ggplot(aes(Date, Time2)) + 
  geom_ribbon(aes(Date, ymin=0, ymax=Sunrise2, group=1), fill="steelblue4", alpha = .5) + 
  geom_ribbon(aes(Date, ymin=Sunset2, ymax=1, group=1), fill="steelblue4", alpha = .5) +
  geom_point(shape=20, alpha=.9) + 
  facet_grid(Mooring ~ month(test.dat$Date, label = T, abbr = F), space = "free_x", scales = "free_x", switch = "x",
             labeller=labeller(Mooring = c('M1' = 'Mooring 1', 'M2' = 'Mooring 2'))) + 
  scale_x_datetime(breaks = my.breaks, 
                   date_labels = "%e", 
                   expand = c(.02,0)) +
  scale_y_continuous(labels = c("00:00", "06:00", "12:00", "18:00", "23:59"),
                     expand = c(.02, 0)) +
  geom_blank() +
  labs(y="Time", x="") +
  theme(panel.spacing.x = unit(0, "cm"),
        strip.placement = "outside",
        strip.background = element_blank(),
        plot.margin = margin(.5, .5, .5, .5, "cm"),
        panel.spacing.y = unit(.3, "cm"),
        panel.grid.major.x = element_line(colour="grey93", size = .2),
        panel.grid.major.y = element_line(colour="grey93", size = .2),
        panel.background = element_blank())

下面是我的数据示例,以使其可再现[编辑]。

structure(list(Date = structure(c(1434758400, 1433894400, 1433635200, 
1433980800, 1439164800, 1439596800, 1441411200, 1444435200, 1435708800, 
1436227200, 1434067200, 1441584000, 1439769600, 1438214400, 1443139200, 
1441497600, 1439683200, 1436918400, 1439683200, 1436918400), class = c("POSIXct", 
"POSIXt")), Mooring = structure(c(1L, 1L, 1L, 5L, 5L, 5L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 1L), .Label = c("M1", 
"M10", "M11", "M12", "M2", "M3", "M4", "M5", "M6", "M7", "M8", 
"M9"), class = "factor"), Time2 = c(0.958333333333333, 0.380555555555556, 
0.447222222222222, 0.10625, 0.378472222222222, 0.717361111111111, 
0.125694444444444, 0.125694444444444, 0.413888888888889, 0.619444444444444, 
0.547222222222222, 0.653472222222222, 0.191666666666667, 0.402777777777778, 
0.344444444444444, 0.366666666666667, 0.389583333333333, 0.85625, 
0.761805555555556, 0.169444444444444), Sunrise2 = c(0.162962962962963, 
0.164363425925926, 0.165648148148148, 0.167268518518519, 0.211458333333333, 
0.218009259259259, 0.243645833333333, 0.290729166666667, 0.166493055555556, 
0.170451388888889, 0.163726851851852, 0.246319444444444, 0.218252314814815, 
0.194699074074074, 0.271921296296296, 0.246967592592593, 0.21931712962963, 
0.180613425925926, 0.21931712962963, 0.17755787037037), Sunset2 = c(0.893229166666667, 
0.889189814814815, 0.887199074074074, 0.889756944444444, 0.852395833333333, 
0.844641203703704, 0.807824074074074, 0.744618055555556, 0.892546296296296, 
0.889895833333333, 0.890335648148148, 0.804224537037037, 0.840601851851852, 
0.86744212962963, 0.773125, 0.807268518518519, 0.843055555555556, 
0.884270833333333, 0.843055555555556, 0.884097222222222)), row.names = c(NA, 
-20L), class = "data.frame")

感谢一百万

2 个答案:

答案 0 :(得分:2)

一种方法是在y轴刻面后面的\ n和\ n之间添加空格:

scale_y_continuous(labels = paste0(c("00:00", "06:00", "12:00", "18:00", "23:59"), "    "),
                     expand = c(.02, 0)) +
facet_grid(Mooring ~ month(test.dat$Date, label = T, abbr = F), space = "free_x",
           scales = "free_x", switch = "x",
           labeller=labeller(Mooring = c('M1' = 'Mooring 1\n', 'M2' = 'Mooring 2\n'))) + 

enter image description here


编辑: 或者,我们可以在任一侧添加虚拟数据:

library(lubridate)
extra_rows <- tibble(
  Date = c(min(test.dat$Date) - lubridate::ddays(5),
           max(test.dat$Date) + lubridate::ddays(5)),
  Month = month(Date, label = T, abbr = F) %>%
    factor(levels = c("May", "June", "July", "August",
                      "September", "October", "November", 
                      "April", "December", "March"))
)

test.dat %>%
  bind_rows(extra_rows) %>%  # <- add in dummy data

  ggplot(aes(Date, Time2)) + 
  # ...

这在6月份效果很好,但是8月份没有更多的日子可以添加到右侧。

enter image description here

答案 1 :(得分:0)

我现在将发布@JonSpring的解决方案!

问题出在以下代码段中:

facet_grid(Mooring ~ month(test.dat$Date, label = T, abbr = F), space = "free_x", ...  

错误是“ test.dat $ Date”。这称为数据集,而没有添加额外的行,这就是发生冲突的原因。我相信我之前已经添加了这个test.dat $,因为它曾经给我一个错误,可能也可以通过在调用函数lubridate::

之前添加month()来解决。

因此最终代码应为:

extra_rows <- tibble(
  Date = c(min(test.dat$Date) - lubridate::ddays(5),
           max(test.dat$Date) + lubridate::ddays(5)),
  Month = month(Date, label = T, abbr = F) %>%
    factor(levels = c("May", "June", "July", "August",
                      "September", "October", "November", 
                      "April", "December", "March")),
  Mooring = "M1"
)

test.dat %>%
  bind_rows(extra_rows) %>%  # <- add in dummy data

  ggplot(aes(Date, Time2)) + # main axes of individual plots: time vs day
  geom_ribbon(aes(Date, ymin=0, ymax=Sunrise2, group=1), fill="steelblue4", alpha = .5) + # GROUP=1 very important, or nothing will happen!
  geom_ribbon(aes(Date, ymin=Sunset2, ymax=1, group=1), fill="steelblue4", alpha = .5) +
  geom_point(shape=20, alpha=.9) + # to specify that we want points, and change settings
  facet_grid(Mooring ~ lubridate::month(Date, label = T, abbr = F), space = "free_x", scales = "free_x", switch = "x",
             labeller=labeller(Mooring = c('M1' = 'Mooring 1', 'M2' = 'Mooring 2'))) + # facet to have several panels, the first one is for the vertical panels, second horizontal
  scale_x_datetime(breaks = my.breaks, 
                   date_labels = "%e", 
                   expand = c(.02,0)) +
  scale_y_continuous(labels = c("00:00", "06:00", "12:00", "18:00", "23:59"),
                     expand = c(.02, 0)) +
  labs(y="Time", x="") +
  theme(panel.spacing.x = unit(0, "cm"),
        strip.placement = "outside",
        strip.background = element_blank(),
        plot.margin = margin(.5, .5, .5, .5, "cm"),
        panel.spacing.y = unit(.3, "cm"),
        panel.grid.major.x = element_line(colour="grey93", size = .2),
        panel.grid.major.y = element_line(colour="grey93", size = .2),
        panel.background = element_blank())
        #plot.background = element_blank())

非常感谢@JonSpring!