Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

r - Issue with colour fill with gganimate and geom_sf

I'm having a problem with gganimate where it does not fill the geom_sf points I am using all of the time. A static version of the plot I'm using works fine:

precincts$margingroup <- cut(precincts$margin, 
                       breaks = breaks, labels = c(1:37))    

pointfig <- ggmap(myMap) + 
      geom_sf(data=centroids, aes(fill=precincts$margin,group=precincts$margingroup), size=precincts$dotsize, pch=21, alpha=1, inherit.aes = FALSE) +
      scale_fill_gradient2(midpoint = 0, low='darkmagenta',
                            min = 'white',
                            high='orange',
                            limits = c(-50,50),
                            oob = scales::squish) +
      geom_shadowtext(mapping = aes(x = longitude, y = latitude, label = name, vjust=vjust), 
                      data = places, size = 5, fontface = "bold") +
      labs(fill='Fidesz Margin, %',caption = "Data from valasztas.hu") +
      ggtitle("Borsod-Abaúj-Zemplén 6th District By-Election by Precinct") + 
      theme_void() +
      theme(legend.box.just = "center") +
      theme(plot.title = element_text(size = 12, face = "bold", vjust=4)) +
      theme(plot.margin = unit(c(1,1,1,1), "cm")) +
      coord_sf()

enter image description here

But when I try to animate it I get an issue with fill. I want the points to appear in order of margin (but I had to group them so that there were fewer than 50 states).

anim <- pointfig + transition_states(precincts$margingroup,
                             transition_length = 1,
                             state_length = 0.45, wrap = FALSE) +
  shadow_wake(wake_length = 0.1, alpha = TRUE)

This results in (small version for SO but you get the idea!):

enter image description here

The colours which appear towards the end are whole the whole thing should look. Any help would be hugely appreciated, thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...