Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Strange behavior of ggplot's geom_polygon when using gganimate

$
0
0

Say that I have these data. One for shading counties of a state and the other for plotting points.

library(tidyverse)
library(gganimate)
devtools::install_github("UrbanInstitute/urbnmapr")
library(urbanmapr)

#1. counties dataset for the shading of the background
data(counties)
#keep only Texas counties
counties <- filter(counties, state_fips==48)

#2. dots dataset for dots over time
dots <- data.frame(group = c(rep(1,3), rep(2,3)), 
                    lat = c(rep(32, 3), rep(33, 3)),
                    long = c(rep(-100, 3), rep(-99,3)), 
                    year = c(1:3, 1:3))

I then plot them (first not using gganimate):

ggplot() +
    geom_polygon(data = counties, aes(long, lat, group = county_fips, fill = as.numeric(county_fips))) +
    scale_fill_gradient(low = "navy", high = "lightskyblue3")   +   
    geom_point(data = dots, aes(long, lat, group=interaction(long, lat), color=year), 
               size=6, show.legend = FALSE) + 
    theme_bw() +
    scale_color_gradientn(colours = c("red", "yellow", "darkgreen")) +
    coord_map() +
    labs(subtitle = paste('Year: {frame_time}')) +  
    theme(plot.subtitle = element_text(hjust = 0.8, vjust=-10, size=30)) +  
    theme(panel.background = element_rect(fill = 'white')) +
    theme(panel.grid = element_blank(),axis.title = element_blank(),
          axis.text = element_blank(),axis.ticks = element_blank(),
          panel.border = element_blank())+
    theme(legend.position = c(0.15, .15)) +
    theme(legend.key.size = unit(2,"line"),legend.title=element_text(size=16), 
          legend.text=element_text(size=14))    +
    labs(fill = "abc")

Here is the result:

enter image description here

The problem comes in when I try to animate the dots using gganimate:

map <- ggplot() +
    geom_polygon(data = counties, aes(long, lat, group = county_fips, fill = as.numeric(county_fips))) +
    scale_fill_gradient(low = "navy", high = "lightskyblue3")   +
    geom_point(data = dots, aes(long, lat, group=interaction(long, lat), color=year), 
               size=6, show.legend = FALSE) + 
    theme_bw() +
    scale_color_gradientn(colours = c("red", "yellow", "darkgreen")) +
    coord_map() +
    labs(subtitle = paste('Year: {frame_time}')) +  
    theme(plot.subtitle = element_text(hjust = 0.8, vjust=-10, size=30)) +  
    theme(panel.background = element_rect(fill = 'white')) +
    theme(panel.grid = element_blank(),axis.title = element_blank(),
          axis.text = element_blank(),axis.ticks = element_blank(),
          panel.border = element_blank())+
    theme(legend.position = c(0.15, .15)) +
    theme(legend.key.size = unit(2,"line"),legend.title=element_text(size=16), 
          legend.text=element_text(size=14))    +
    labs(fill = "abc") +    
    transition_time(year) +
    shadow_mark(size=6)     

anim_save("output/test.gif", map, end_pause=6, width = 800, height = 800, duration=8)

Note that it is exactly the same except for the transition_time and shadow_mark lines. Here is the result:

enter image description here

The background colors are VERY different. It might be the case that the colors are reversed or something; I am not sure. However, in my real example using many more dots and different values for the shading, the shading has little if any resemblance to the actual data. What in the world is going on and how can I fix it?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>