[英]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). 我想在R中制作一个条形图,其中两组条形之间存在间隙(但不是一组条形图)。 I have data here with each of 30 basketball teams and their average points:
我这里有30个篮球队各取其平均点的数据:
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: 有了它,我就可以绘制出30个柱形图,并且柱形图的排序越来越多:
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. 相反,我想简单地绘制最低值的5条和最高值的5条,两组柱之间有一个“间隙”,表明其他20条在中间,但已经带走。
I'm not quite sure if geom_bar has a way to handle this, or if i need to edit my dataframe. 我不太确定geom_bar是否可以处理此问题,或者是否需要编辑数据框。 Any help with this is appreciated!!
任何帮助,不胜感激!
How about making use facet_wrap
? 使用
facet_wrap
怎么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
. 您可以通过调整面板之间的缝隙
panel.spacing
内theme
。
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.