R 如何防止酒吧突出
假设我使用以下代码创建了一个条形图竞赛,这是从这些问题的惊人答案中衍生出来的,并且:R 如何防止酒吧突出,r,ggplot2,bar-chart,gganimate,R,Ggplot2,Bar Chart,Gganimate,假设我使用以下代码创建了一个条形图竞赛,这是从这些问题的惊人答案中衍生出来的,并且: 库(gapminder) 库(gganimate) 图书馆(tidyverse) 图书馆(GGSTANTE) 间隙更平滑% 过滤器(大陆==“亚洲”)%>% 按(国家)划分的组别%>% 完成(年份=完整_seq(年份,1))%>% 突变(gdpPercap=样条曲线(x=year,y=gdpPercap,xout=year)$y)%>% 组别(年份)%>% 变异(秩=最小秩(-gdpPercap)*1)%>%
库(gapminder)
库(gganimate)
图书馆(tidyverse)
图书馆(GGSTANTE)
间隙更平滑%
过滤器(大陆==“亚洲”)%>%
按(国家)划分的组别%>%
完成(年份=完整_seq(年份,1))%>%
突变(gdpPercap=样条曲线(x=year,y=gdpPercap,xout=year)$y)%>%
组别(年份)%>%
变异(秩=最小秩(-gdpPercap)*1)%>%
解组()%>%
按(国家)划分的组别%>%
完成(年份=完整_seq(年份,.5))%>%
突变(gdpPercap=样条曲线(x=year,y=gdpPercap,xout=year)$y)%>%
变异(秩=大约(x=年,y=秩,xout=年)$y)%>%
解组()%>%
安排(国家,年份)%>%
过滤器(年份=1999)%>%
filter(rank原因是它试图在两个国家(短暂地)重叠时将它们的条堆叠起来
可以通过将position=“identity”
添加到您的geom\u colh()
来修复此问题:
结果:
library(gapminder)
library(gganimate)
library(tidyverse)
library(ggstance)
gap_smoother <- gapminder %>%
filter(continent == "Asia") %>%
group_by(country) %>%
complete(year = full_seq(year, 1)) %>%
mutate(gdpPercap = spline(x = year, y = gdpPercap, xout = year)$y) %>%
group_by(year) %>%
mutate(rank = min_rank(-gdpPercap) * 1) %>%
ungroup() %>%
group_by(country) %>%
complete(year = full_seq(year, .5)) %>%
mutate(gdpPercap = spline(x = year, y = gdpPercap, xout = year)$y) %>%
mutate(rank = approx(x = year, y = rank, xout = year)$y) %>%
ungroup() %>%
arrange(country,year) %>%
filter(year<=2007 & year>=1999) %>%
filter(rank<=8)
p <- ggplot(gap_smoother, aes(y=rank,
fill = as.factor(country), color = as.factor(country))) +
geom_colh(aes(x=gdpPercap), width=0.9, alpha = 0.8, color = NA) +
scale_y_reverse(labels = scales::comma) +
guides(color = FALSE, fill = FALSE) +
coord_cartesian(clip='off') +
theme(panel.background = element_rect(fill = "white")) +
geom_text(aes(x = 0, label = paste(country, " ")), vjust = 0.2, hjust = 1) +
geom_text(aes(x = gdpPercap,
label = scales::comma(round(gdpPercap), accuracy=1)), hjust = 0, nudge_x = 0 ) +
labs(title='{closest_state %>% as.numeric %>% floor}',
x = "GFP per capita", y = "") +
theme(plot.title = element_text(hjust = 0, size = 22),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
plot.margin = margin(1,1,1,4, "cm"),
axis.line.y = element_blank()) +
transition_states(year, transition_length = 1, state_length = 0) +
#enter_grow() +
#exit_shrink() +
ease_aes('linear')
animate(p, fps = 10, duration = 5, width = 600, height = 500)
#anim_save("output/bar.gif", p, end_pause=8, width = 600, height = 500, duration=5, nframes=50)
geom_colh(aes(x=gdpPercap), position = "identity", width=0.9, alpha = 0.8, color = NA) +