在两页上拆分大条形图

时间:2019-04-15 16:53:00

标签: r ggplot2

我有一个大条形图:

enter image description here

我想将其分成两部分,并将其打印到两页上,如下图所示,但两边的比例尺均相等。以下一项是通过以下方式完成的:

library(dplyr)
library(gridExtra)

p1 = d %>%
  slice(1:30) %>%
  ggplot(aes(reorder(shop_id, sales), sales)) +
  geom_bar(stat = "identity") +
  labs(x = "shop") +
  theme_minimal() +
  coord_flip()

p2 = d %>%
  slice(31:60) %>%
  ggplot(aes(reorder(shop_id, sales), sales)) +
  geom_bar(stat = "identity") +
  labs(x = "shop") +
  theme_minimal() +
  coord_flip()

grid.arrange(p1,p2, ncol = 2)

...但是必须有更好的方法。

enter image description here

    structure(list(shop_id = 0:59, sales = c(1.18748097798519, 1.1114829165199, 
1.17810011157708, 1.1105671314429, 1.14905078186287, 1.12003981246235, 
1.21564666174709, 1.15465941180522, 1.05363423212192, 4.22980538523061, 
1.14609524699724, 1.14629258517034, 2.11788781921946, 1.10878590664273, 
1.25409015927959, 1.19643427265548, 1.16875260742595, 1.12583877995643, 
1.23031544141131, 1.14933266573829, 3.27678571428571, 1.17936456057661, 
1.32565919795748, 1.10656326296137, 1.2046688791673, 1.2999183252375, 
1.25932109070673, 1.29697435605414, 1.29755895214928, 1.19274758760792, 
1.19598898938262, 1.31888590877455, 1.10494526236316, 1.09051123930774, 
1.12152294853964, 1.18087090426897, 1.07843137254902, 1.16696099702306, 
1.17110381848608, 1.23638392857143, 1.16114634719286, 1.17530440584269, 
1.32659057417188, 1.28832544167812, 1.13680748798381, 1.16728427739545, 
1.19102546704664, 1.19299761883764, 1.15255413659078, 1.0783014701243, 
1.16977889616866, 1.09754011658002, 1.14348765573997, 1.16507624572476, 
1.29488430443267, 1.82311829503293, 1.12226007215443, 1.20164696665191, 
1.14407693061407, 1.16350812197207)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -60L))

2 个答案:

答案 0 :(得分:2)

这是使用facet_wrap的一种解决方案。我选择在事实之后隐藏构面信息,但是您可以根据需要轻松地进行更改:

x <-  structure(list(shop_id = 0:59, sales = c(1.18748097798519, 1.1114829165199, 
                                               1.17810011157708, 1.1105671314429, 1.14905078186287, 1.12003981246235, 
                                               1.21564666174709, 1.15465941180522, 1.05363423212192, 4.22980538523061, 
                                               1.14609524699724, 1.14629258517034, 2.11788781921946, 1.10878590664273, 
                                               1.25409015927959, 1.19643427265548, 1.16875260742595, 1.12583877995643, 
                                               1.23031544141131, 1.14933266573829, 3.27678571428571, 1.17936456057661, 
                                               1.32565919795748, 1.10656326296137, 1.2046688791673, 1.2999183252375, 
                                               1.25932109070673, 1.29697435605414, 1.29755895214928, 1.19274758760792, 
                                               1.19598898938262, 1.31888590877455, 1.10494526236316, 1.09051123930774, 
                                               1.12152294853964, 1.18087090426897, 1.07843137254902, 1.16696099702306, 
                                               1.17110381848608, 1.23638392857143, 1.16114634719286, 1.17530440584269, 
                                               1.32659057417188, 1.28832544167812, 1.13680748798381, 1.16728427739545, 
                                               1.19102546704664, 1.19299761883764, 1.15255413659078, 1.0783014701243, 
                                               1.16977889616866, 1.09754011658002, 1.14348765573997, 1.16507624572476, 
                                               1.29488430443267, 1.82311829503293, 1.12226007215443, 1.20164696665191, 
                                               1.14407693061407, 1.16350812197207)), class = c("tbl_df", "tbl", 
                                                                                               "data.frame"), row.names = c(NA, -60L))


x$rank <- rank(-x$sales)
x$group <- ifelse(x$rank <= 30, 1,2)


library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.4.4

ggplot(x, aes(reorder(shop_id, sales), sales)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  facet_wrap(~group, scales = "free_y") +
  xlab("Store") +
  theme(strip.background = element_blank(), strip.text = element_blank())

reprex package(v0.2.1)于2019-04-15创建

答案 1 :(得分:2)

您可以使用slice来细分数据,然后使用scale_y_continuous手动设置休息时间。

data %>%
 slice(1:30) %>%
 ggplot(aes(reorder(shop_id, sales), sales)) +
 geom_col() +
 coord_flip() +
 scale_y_continuous(limits = c(0, 4))

data %>%
 slice(31:60) %>%
 ggplot(aes(reorder(shop_id, sales), sales)) +
 geom_col() +
 coord_flip() +
 scale_y_continuous(limits = c(0, 4))

enter image description here

如果您将经常使用它,也可以将其转换为函数:

slice_and_dice <- function(data, rows) {
 data %>%
 slice(rows) %>%
 ggplot(aes(reorder(shop_id, sales), sales)) +
 geom_col() +
 coord_flip() +
 scale_y_continuous(limits = c(0, 4))
}