ggplot2:绘图之外的注释边距

时间:2018-03-02 12:50:16

标签: r ggplot2

我几乎遇到了与此问题完全相同的问题:Multi-row x-axis labels in ggplot line chart。接受的答案大部分都适用于我,但根据我的情节大小,注释有时会从我的情节中消失。

我已经尝试过相对于y尺度的情节和传奇边缘,但它并没有像我希望的那样奏效。这是我到目前为止所得到的:

library(magrittr)
library(ggplot2)

test <- structure(list(
    area = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
                       2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
                     .Label = c("A", "B", "C"),
                     class = "factor"),
    Year = c(2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2017L,
             2017L, 2017L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L,
             2016L, 2017L, 2017L, 2017L),
    Quarter = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L,
                2L, 3L, 4L, 1L, 2L, 3L),
    rate = c(0.52, 0.35, 0.23, 0.36, 0.21, 0.17, 0.33, 0.26, 0.3, 0.31, 0.24,
             0.24, 0.29, 0.42, 0.15, 0.36, 0.33, 0.41, 0.27, 0.34, 0.33, 0.25)),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA, -22L),
    .Names = c("area", "Year", "Quarter", "rate"))

yMax <- max(test$rate) * 1.1
yMin <- yMax/7

fig2 <- (ggplot(test,
                mapping = aes(x = interaction(Year, Quarter, lex.order = TRUE),
                              y = rate, group = area, color = area)) +
             geom_line() +
             coord_cartesian(ylim = c(0, yMax), expand = TRUE) +
             scale_x_discrete(labels = paste0("Q", rep(1:4, 3))) +
             scale_y_continuous(name = "Rate", expand = c(0,0)) +
             annotate(geom = "text", label = unique(test$Year),
                      x = 2.5 + 4 * (0:2),
                      y = -yMin,
                      size = 5, vjust = 0) +
             annotate(geom = "segment",
                      x = c(4.5, 8.5), xend = c(4.5, 8.5),
                      y = 0, yend = -yMin * 1.1) +
             theme(plot.margin = margin(b = yMin/2, unit = "native"),
                   axis.title.x = element_blank(),
                   panel.background = element_blank(),
                   axis.line = element_line(),
                   legend.position = "bottom",
                   legend.margin = margin(t = 30, unit = "native"),
                   legend.background = element_rect(fill = alpha("red", 0.5)))
) %>%
    ggplotGrob() %>% 
    (function(x) {
        x$layout$clip[x$layout$name == "panel"] <- "off"
        return(x)
    }); grid::grid.draw(fig2)

在一个尺寸上,情节看起来很好,但是如果我增加情节的高度,那么年份就会开始与传说冲突,如果我让它更高,它们就会从底部消失。

我很困惑为什么图例边距不会随着情节的大小而缩放,因为如果它确实如此,我认为这将解决我的问题。另外,我非常困惑为什么30个原生单位的图例边距与~0.04原生单位的绘图边距大小相同?

按照我的意愿布置情节(红色传奇背景只是让我看到发生了什么): regular height

较高的情节开始出现问题: taller (problem plot)

1 个答案:

答案 0 :(得分:0)

@Oliver将plot.margin单位更改为linesincm对我有用,问题在于native单位的缩放。这个解决方案对你有用吗?

fig2 <- (ggplot(test,
                mapping = aes(x = interaction(Year, Quarter, lex.order = TRUE),
                              y = rate, group = area, color = area)) +
           geom_line() +
           coord_cartesian(ylim = c(0, yMax), expand = TRUE) +
           scale_x_discrete(labels = paste0("Q", rep(1:4, 3))) +
           scale_y_continuous(name = "Rate", expand = c(0,0)) +
           annotate(geom = "text", label = unique(test$Year),
                    x = 2.5 + 4 * (0:2),
                    y = -yMin,
                    size = 5, vjust = 0) +
           annotate(geom = "segment",
                    x = c(4.5, 8.5), xend = c(4.5, 8.5),
                    y = 0, yend = -yMin * 1.1) +
           theme(plot.margin = margin(c(1, 1, 3, 1), unit="lines"),
                 axis.title.x = element_blank(),
                 panel.background = element_blank(),
                 axis.line = element_line(),
                 legend.position = "bottom",
                 legend.margin = margin(c(3, 1, 1, 1), unit="lines"),
                 legend.background = element_rect(fill = alpha(0.5)))
                 ) %>%
  ggplotGrob() %>% 
  (function(x) {
    x$layout$clip[x$layout$name == "panel"] <- "off"
    return(x)
  }); grid.draw(fig2)

我将legend.background = element_rect(fill = alpha("red", 0.5))更改为legend.background = element_rect(fill = alpha(0.5)),因为上边距呈红色阴影并与辅助轴标题重叠。