在两页上拆分大条形图

问题描述 投票:2回答:2

我有一个大条形图:

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))
r ggplot2
2个回答
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创建于2019-04-15(v0.2.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))
}
© www.soinside.com 2019 - 2024. All rights reserved.