> For the complete documentation index, see [llms.txt](https://nikhilc52.gitbook.io/making-animated-visualizations-in-r/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nikhilc52.gitbook.io/making-animated-visualizations-in-r/appendix/basic-animations/animated-maps.r.md).

# animated-maps.R

Code used to make the graph found within "Animated Maps"

<figure><img src="https://3504879863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgCu4lOpNUmrlco6h3cA7%2Fuploads%2FKHRqxbLpMOoTMZBvWdW0%2Ffinal.gif?alt=media&amp;token=8c1a7373-ac15-4bc4-8039-67de2149d2f5" alt="" width="563"><figcaption></figcaption></figure>

```r
library(ggplot2)
library(maps)
library(sf)
library(dplyr)
library(gganimate)
library(tidygeocoder)

usa <- st_as_sf(maps::map("state", fill=TRUE, plot=FALSE))

txhousing_data <- txhousing

cities <- data.frame(unique(txhousing_data$city))
names(cities)[names(cities) == 'unique.txhousing_data.city.'] <- 'city'

cities <- geo_osm(paste0(cities$city, ", Texas"))

txhousing_data$city <- paste0(txhousing_data$city,", Texas")
txhousing_data <- left_join(txhousing_data, cities, by=join_by('city'=='address'))

sf_txhousing_data <- txhousing_data |> 
  filter(!is.na(long)) |> 
  filter(!is.na(lat)) |> 
  st_as_sf(coords = c('long','lat')) |> 
  st_set_crs(4326)

animation <- ggplot()+
  geom_sf(data=usa)+
  geom_sf(data=sf_txhousing_data, aes(size=listings), shape=1, color="gray")+
  geom_sf(data=sf_txhousing_data, aes(size=sales, color=median))+
  viridis::scale_color_viridis(option="B", label=scales::comma)+
  scale_size_continuous(label=scales::comma, range = c(1,15))+
  theme_void()+
  labs(title="Texas Housing Data from 2000-2015",
       subtitle="The outer circle is how many houses were listed.<br>The inner circle is how many houses were sold and for what price.
       <br><br>Year: {as.integer(frame_time)}<br>",
       caption="Nikhil Chinchalkar for Princeton University | TXHousing | 2024",
       color="Median Sale Price", 
       size="Number of Listings/Sales")+
  theme(plot.title = ggtext::element_markdown(size = 22, hjust =0.5, face = "bold"), 
        plot.subtitle = ggtext::element_markdown(size = 15, hjust =0.5, face = "bold"))+
  coord_sf(xlim = c(-107, -90), ylim = c(25, 37))+
  transition_time(date)

animate(animation, fps=10, duration=15, end_pause=30, height = 8,
        width = 9, units = "in", res = 200)

```
