简体   繁体   中英

geom_bar in ggplot - add a break / gap between two bars

I would like to make a bar chart in R where there is a gap between two groups of bars, (but not a grouped bar chart). I have data here with each of 30 basketball teams and their average points:

dput(mydf)
structure(list(thisTeamAbb = c("ATL", "BKN", "BOS", "CHA", "CHI", 
"CLE", "DAL", "DEN", "DET", "GSW", "HOU", "IND", "LAC", "LAL", 
"MEM", "MIA", "MIL", "MIN", "NOP", "NYK", "OKC", "ORL", "PHI", 
"PHX", "POR", "SAC", "SAS", "TOR", "UTA", "WAS"), pts = c(1.197, 
1.138, 1.016, 1.127, 1.196, 1.144, 1.21, 1.197, 1.155, 1.107, 
1.126, 1.138, 1.282, 1.105, 1.096, 1.205, 1.121, 1.125, 1.205, 
1.208, 1.208, 1.098, 1.056, 1.167, 1.039, 1.128, 0.99, 1.171, 
0.987, 1.127)), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))

> head(mydf)
# A tibble: 6 x 2
  thisTeamAbb   pts
  <chr>       <dbl>
1 ATL          1.20
2 BKN          1.14
3 BOS          1.02
4 CHA          1.13
5 CHI          1.20
6 CLE          1.14

and with it i am able to make this plot with 30 bars, with the bars sorted increasingly:

mydf %>% ggplot() + 
  geom_bar(aes(x = reorder(thisTeamAbb, pts), y = pts), stat = "identity")

在此处输入图片说明

...however this graph has too many bars.

Instead, I'd like to simply graph the 5 bars with the lowest values, and 5 bars with the highest values, with a 'gap' between the two sets of bars that indicates that the other 20 bars belong in the middle but have been taken out.

I'm not quite sure if geom_bar has a way to handle this, or if i need to edit my dataframe. Any help with this is appreciated!!

How about making use facet_wrap ?

df %>%
    mutate(region = case_when(
        min_rank(desc(pts)) <= 5 ~ "high",
        min_rank(pts) <= 5 ~ "low",
        TRUE ~ "middle")) %>%
    filter(region %in% c("low", "high")) %>%
    mutate(
        thisTeamAbb = reorder(thisTeamAbb, pts),
        region = factor(region, c("low", "high"))) %>%
    ggplot() +
    geom_col(aes(thisTeamAbb, pts)) +
    facet_wrap(~ region, scale = "free_x") +
    theme(panel.spacing = unit(2, "lines"))

在此处输入图片说明

You can adjust the gap between panels via panel.spacing inside theme .


Update

With a bit of extra "theme-ing"

df %>%
    mutate(region = case_when(
        min_rank(desc(pts)) <= 5 ~ "high",
        min_rank(pts) <= 5 ~ "low",
        TRUE ~ "middle")) %>%
    filter(region %in% c("low", "high")) %>%
    mutate(
        thisTeamAbb = reorder(thisTeamAbb, pts),
        region = factor(region, c("low", "high"))) %>%
    ggplot() +
    geom_col(aes(thisTeamAbb, pts)) +
    facet_wrap(~ region, scale = "free_x", strip.position = "bottom") +
    theme_minimal() +
    theme(
        panel.spacing = unit(5, "lines"),
        strip.placement = "outside")

在此处输入图片说明

library(tidyverse)

my_df2 %>%
  mutate(rank = <-
  bind_rows(
    my_df %>% top_n(wt = pts, 5),
    my_df %>% slice(1) %>% mutate(thisTeamAbb = "Middle 20", pts = NA_real_),
    my_df %>% top_n(wt = pts, -5)  # Includes 6 b/c ties....
) %>%
# Here I make the team name into sorted factor, with Middle 20 in the middle
mutate(thisTeamAbb = thisTeamAbb %>% 
           fct_reorder(pts) %>%
           fct_relevel("Middle 20", after = 5))

my_df2 %>% ggplot() + 
  geom_bar(aes(x = thisTeamAbb, y = pts), stat = "identity")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM