高亮出现的元素

问题描述 投票:0回答:1

不确定是否有可能突出显示元素(边框)的功能,比方说红色,然后在后续帧中将其更改为不同的颜色,如黑色。也可以展示如何更改边框的大小。

例子:

# load packages 
pack <- c('gganimate','rnaturalearth','sf','tidyverse')
lapply(pack,require,character.only=T)

# create the gif for African content
africa <- ne_countries(continent = "africa",returnclass ="sf")
# get 10 countries
country10 <- st_as_sf(head(africa, 10))
# set the factor level for the apperance order of the countries
country10$order <- factor(country10$sovereignt,levels=country10$sovereignt)

#  merge geometry for africa
africa$un <- rep.int(1,length(africa$featurecla))
sf_use_s2(FALSE)
africa <- africa %>%
  group_by(un) %>%
  summarise(geometry=st_union(geometry))


a <- ggplot(data = africa) + 
  geom_sf() + 
  coord_sf()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        panel.background = element_rect(fill = "lightgrey", colour = NA),
        plot.background = element_rect(fill = "lightgrey",colour = NA),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+
  geom_sf(data = country10, col = "black", fill = "transparent") +
  transition_states(order) + shadow_mark()

# output Africa gif
animate(a,fps=10, height = 4,
        width = 3, units = "cm", res = 600)
anim_save("africa.gif")

)

r ggplot2 gif animated-gif gganimate
1个回答
0
投票

展开评论--

如果你为每个国家对象分配一个“年龄”,那么每次通过你的绘图循环,你都可以将“年龄”与循环计数器进行比较,并根据该差异选择颜色。我认为这将需要对您的代码进行少量重写,这样您就不会在对

country10
的给定调用中包含
geom_sf
的所有元素。循环更新您的
grob
,一次添加一个国家。

© www.soinside.com 2019 - 2024. All rights reserved.