Ggplot构面包裹,仅留下上部和侧面标签

时间:2018-03-15 23:35:13

标签: r ggplot2

我在创建带有方面的情节时遇到问题。它有57个地块,所以我非常考虑空间。我想把车站的名字留在最顶层,多年留在两侧。有没有办法只为特定方面设置标签?

以下是一个示例图:

ggplot(x)+ 
  geom_col(aes(x = days, y = irish_rules),size= 1)+
  scale_y_continuous(limits = c(0, 40) ) +
  theme(axis.title.x = element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+
  facet_wrap(~id, ncol = 3)+
  theme(strip.background = element_blank())

enter image description here

以下是数据样本:

x <-structure(list(id = c("1983_Birr", "1983_Birr", "1983_Birr", 
"1983_Birr", "1983_Birr", "1983_Birr", "1983_Birr", "1983_Birr", 
"1983_Birr", "1983_Birr", "1983_Birr", "1983_Birr", "1983_Birr", 
"1983_Birr", "1983_Birr", "1983_Casement", "1983_Casement", "1983_Casement", 
"1983_Casement", "1983_Casement", "1983_Casement", "1983_Casement", 
"1983_Casement", "1983_Casement", "1983_Casement", "1983_Casement", 
"1983_Casement", "1983_Casement", "1983_Casement", "1983_Casement", 
"1983_Kilkenny", "1983_Kilkenny", "1983_Kilkenny", "1983_Kilkenny", 
"1983_Kilkenny", "1983_Kilkenny", "1983_Kilkenny", "1983_Kilkenny", 
"1983_Kilkenny", "1983_Kilkenny", "1983_Kilkenny", "1983_Kilkenny", 
"1983_Kilkenny", "1983_Kilkenny", "1983_Kilkenny", "1984_Birr", 
"1984_Birr", "1984_Birr", "1984_Birr", "1984_Birr", "1984_Birr", 
"1984_Birr", "1984_Birr", "1984_Birr", "1984_Birr", "1984_Birr", 
"1984_Birr", "1984_Birr", "1984_Birr", "1984_Birr", "1984_Casement", 
"1984_Casement", "1984_Casement", "1984_Casement", "1984_Casement", 
"1984_Casement", "1984_Casement", "1984_Casement", "1984_Casement", 
"1984_Casement", "1984_Casement", "1984_Casement", "1984_Casement", 
"1984_Casement", "1984_Casement", "1984_Kilkenny", "1984_Kilkenny", 
"1984_Kilkenny", "1984_Kilkenny", "1984_Kilkenny", "1984_Kilkenny", 
"1984_Kilkenny", "1984_Kilkenny", "1984_Kilkenny", "1984_Kilkenny", 
"1984_Kilkenny", "1984_Kilkenny", "1984_Kilkenny", "1984_Kilkenny", 
"1984_Kilkenny"), short_date = structure(c(4953, 4954, 4955, 
4956, 4957, 4958, 4959, 4960, 4961, 4962, 4963, 4964, 4965, 4966, 
4967, 4953, 4954, 4955, 4956, 4957, 4958, 4959, 4960, 4961, 4962, 
4963, 4964, 4965, 4966, 4967, 4953, 4954, 4955, 4956, 4957, 4958, 
4959, 4960, 4961, 4962, 4963, 4964, 4965, 4966, 4967, 5320, 5321, 
5322, 5323, 5324, 5325, 5326, 5327, 5328, 5329, 5330, 5331, 5332, 
5333, 5334, 5320, 5321, 5322, 5323, 5324, 5325, 5326, 5327, 5328, 
5329, 5330, 5331, 5332, 5333, 5334, 5320, 5321, 5322, 5323, 5324, 
5325, 5326, 5327, 5328, 5329, 5330, 5331, 5332, 5333, 5334), class = "Date"), 
    irish_rules = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    3, 16, 24, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 
    0, 0, 0, 9, 13, 0, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 0, 0), 
    days = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
    13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
    12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
    11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
    9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 
    5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L)), .Names = c("id", 
"short_date", "irish_rules", "days"), row.names = c(NA, -90L), vars = "id", drop = TRUE, indices = list(
    0:14, 15:29, 30:44, 45:59, 60:74, 75:89), group_sizes = c(15L, 
15L, 15L, 15L, 15L, 15L), biggest_group_size = 15L, labels = structure(list(
    id = c("1983_Birr", "1983_Casement", "1983_Kilkenny", "1984_Birr", 
    "1984_Casement", "1984_Kilkenny")), row.names = c(NA, -6L
), class = "data.frame", vars = "id", drop = TRUE, .Names = "id"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:1)

您需要separateid列添加到yearstation,然后使用facet

library(tidyverse)

x1 <- x %>% 
  as.tibble() %>% 
  separate(id, c("year", "station"))
x1

#> # A tibble: 90 x 5
#>    year  station short_date irish_rules  days
#>    <chr> <chr>   <date>           <dbl> <int>
#>  1 1983  Birr    1983-07-25          0.     1
#>  2 1983  Birr    1983-07-26          0.     2
#>  3 1983  Birr    1983-07-27          0.     3
#>  4 1983  Birr    1983-07-28          0.     4
#>  5 1983  Birr    1983-07-29          0.     5
#>  6 1983  Birr    1983-07-30          0.     6
#>  7 1983  Birr    1983-07-31          0.     7
#>  8 1983  Birr    1983-08-01          0.     8
#>  9 1983  Birr    1983-08-02          0.     9
#> 10 1983  Birr    1983-08-03          0.    10
#> # ... with 80 more rows

ggplot(x1) + 
  geom_col(aes(x = days, y = irish_rules),size= 1)+
  scale_y_continuous(limits = c(0, 40) ) +
  theme(axis.title.x = element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+
  facet_grid(year ~ station)+
  theme(strip.background = element_blank())

reprex package(v0.2.0)创建于2018-03-15。