> 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-line-graph.r.md).

# animated-line-graph.R

Code used to make the graph found within "Animated Line Graphs"

<figure><img src="https://3504879863-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgCu4lOpNUmrlco6h3cA7%2Fuploads%2Fbw0uHHnXDuvn7Mh0qhd8%2Ffinal.gif?alt=media&amp;token=f00e9381-1d5a-46c9-98e3-8e15c1b3c39f" alt="" width="563"><figcaption></figcaption></figure>

```r
library(ggplot2)
library(dplyr)
library(gganimate)

txhousing_data <- txhousing

txhousing_data <- txhousing_data |> 
  group_by(date) |> 
  filter(!is.na(sales)) |> 
  filter(!is.na(listings)) |> 
  summarise(
    sales = sum(sales),
    listings = sum(listings)
  )

animation <- ggplot(txhousing_data)+
  geom_line(aes(x=date, y=sales, color="Sales"))+
  geom_line(aes(x=date, y=listings, color="Listings"))+
  scale_color_manual(breaks = c("Sales", "Listings"), values=c("#1f948b","#482071"))+
  scale_y_continuous(label=scales::comma)+
  theme_minimal()+
  labs(title="Texas Housing Market from 2000-2015",
       subtitle="House sales are cyclical and somewhat consistent, listings are more volatile.",
       caption="Nikhil Chinchalkar for Princeton University | TXHousing | 2024",
       color="",
       x="Year",
       y="Number of Sales/Listings")+
  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"))+
  transition_reveal(date)

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

```
