Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
of ggplot2 3.3.4.
elect24<-read_xlsx("C:/Users/Administrator/Downloads/Us Election data animated map/Election2024.xlsx")comb<-left_join(elect24, state, by ="region")ggplot(data=comb, aes(x=long, y=lat, fill=region, group=group)) +geom_polygon(aes(fill=party) , color ="black") +scale_fill_discrete(labels=c("REPUBLICAN","DEMOCRAT"),type=c("darkblue","red"))+guides(fill=FALSE) +theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(),axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(),panel.background =element_rect(fill ="lightblue"),panel.grid.minor =element_line(color="lightblue"),panel.grid.major=element_line(color="lightblue"),) +coord_fixed(1.3)
# 1. Read Dataelect <-read_xlsx("C:/Users/Administrator/Downloads/Us Election data animated map/Election.xlsx")# 2. CRITICAL FIX: Convert region to lowercase to match 'state' dataelect$region <-tolower(elect$region) # 3. Join# Use inner_join to automatically drop rows that don't matchcomb <-inner_join(state, elect, by ="region")
Warning in inner_join(state, elect, by = "region"): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 1 of `x` matches multiple rows in `y`.
ℹ Row 1 of `y` matches multiple rows in `x`.
ℹ If a many-to-many relationship is expected, set `relationship =
"many-to-many"` to silence this warning.
# 4. Create Plotp14 <-ggplot(data = comb, aes(x = long, y = lat, group = group, fill = party)) +geom_polygon(color ="black", size =0.1) +# FIX: Use scale_fill_manual for specific colorsscale_fill_manual(values =c("REPUBLICAN"="darkblue", "DEMOCRAT"="red")) +guides(fill =FALSE) +theme_void() +# theme_void is a shortcut for removing axes/gridstheme(panel.background =element_rect(fill ="lightblue"),plot.title =element_text(hjust =0.5),plot.subtitle =element_text(hjust =0.5) ) +coord_fixed(1.3)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# 6. Render and Save# Ensure nframes is high enough for smooth playback (default is 100)finalmap <-animate(anim, fps =100) image_write(finalmap, path="C:/Users/Administrator/Downloads/Us_Election_map.gif")