1. Prepare test data
raw.data = expand.grid(x=seq(min(0), max(100),by=1),
y=seq(min(50), max(150),by=1)) %>%
mutate(label = ifelse(y > 100,"a","b")) %>%
mutate(label = ifelse(y > 100 & x >= 50,"c",label)) %>%
mutate(label = ifelse(y <= 100 & x < 50,"d",label))
2. Make Bins
animation:::saveGIF(movie.name= "bins-change.gif",{
for (bins.size in rev(seq(2.5,50,5)) ) {
p <- raw.data %>% mutate(bins.x =cut(x, breaks = !!(bins.size),include.lowest=TRUE),
bins.y =cut(y, breaks = !!(bins.size),include.lowest=TRUE)) %>%
group_by(bins.x,bins.y) %>%
summarise(rx_bin = mean(x),
ry_bin = mean(y),
label = sample(label,1),.groups = 'drop') %>%
ggplot() +
geom_point(aes(x=rx_bin,y=ry_bin,color = label),size = 100/bins)+
scale_color_brewer(palette = "Set2")+
coord_equal(xlim = c(0,100),ylim = c(50,150))+
theme_classic() + theme(legend.position = "none",plot.title = element_text(size = 40,hjust = .5)) +
labs(title = paste0("BINS::",bins),x = "",y = "")
print(p)
}
}, interval = 1, ani.width = 1000, ani.height = 1000,loop = 10)
3.Result show
bins-change.gif
网友评论