A look at the big event the world is watching in 2024: the US presidential election - R Coding Visualization

How much can you trust the US presidential election polls? Predictions are mixed, battleground states are close, and you're wondering who will win this hotly contested election. In this post, we'll look at national and state-by-state polling numbers, as well as how to visualize the data with R code. Stay tuned for a big-picture look at the US electoral system and how to understand the current polling situation!
Anatomy of a US presidential election: Why do battleground states matter?
The U.S. presidential election was held in the 'Electoral College votingThe 50 states and the District of Columbia are each allocated electoral votes, and the number of electoral votes varies based on the population size of each state. For example, California has the most electoral votes, 55, while Wyoming has just three. The candidate who wins the most votes in each state takes all of that state's electoral votes (Winner-take-all) in most cases.
This system explains why results in certain states, called "battleground states," are so important, not just the big states. Contendersis a state that often fluctuates in elections, and in this election, the Wisconsin, Nevada, Pennsylvania, etc.belongs here. The entire Wins 270 of the electoral votes to become presidentso the outcome of these states can make a big difference in the direction of the election.
For reference, here's how many electoral votes the seven battleground states in this election have: Combined, they total 93 electoral votes, a significant portion of the 270 electoral votes needed to win the presidency.
| Contenders | Number of electoral votes (people) |
| Wisconsin | 10 |
| Nevada | 6 |
| Pennsylvania | 19 |
| Michigan | 15 |
| North Carolina | 16 |
| Georgia | 16 |
| Arizona | 11 |
Battleground state approval ratings: Trump vs. Harris, the truth about a close race
In the toss-up states, the candidates are split very slightly. For example, Harris has a 0.21 TP3T advantage in Wisconsin and Trump has a 0.21 TP3T advantage in Nevada. To summarize the situation in each state, here's how it looks like. This data is based on the Election Analytics Sites 538from the
| Contenders | Dominant candidate | Support Gap (%p) |
| Wisconsin | Harris | 0.2 |
| Nevada | Trump | 0.2 |
| Pennsylvania | Trump | 0.3 |
| Michigan | Harris | 0.4 |
| North Carolina | Trump | 1.3 |
| Georgia | Trump | 1.5 |
| Arizona | Trump | 1.8 |
Polling error vs. reality, what should I believe?
There is always a certain margin of error in polls, especially in US presidential polls, where the margin of error is estimated to be 3.4%p on average in swing states. To make this easier to understand visually, we'll use R code to visualize the data.

Visualize the US presidential election polling gap with R code
In this lesson, we'll use R code to visualize polling data for the battleground states, which gives us a quick glance at the polling gap and margin of error for each state.
#Load the required packages
library(ggplot2)
#Prepare the data
data 0)) +
geom_bar(stat = "identity", position = "dodge", color = "black", width = 0.7) +
geom_errorbar(aes(ymin = Margin - Poll_Error, ymax = Margin + Poll_Error), width = 0.2) +
scale_fill_manual(values = c("red", "blue"), labels = c("Trump Leads", "Harris Leads")) +
labs(title = "Polling gaps and margins of error in battleground states",
x = "State", y = "Polling Gap (%)") +
theme_minimal()
theme(legend.position = "top") +
guides(fill = guide_legend(title = "Dominant candidate"))Line-by-line code description
- Load a package: ggplot2 packageThis package is most commonly used to visualize data in R.
- Prepare data: Generate data, including the name of the race, the margin of victory, and the polling margin of error.
- Set up a basic graph structure: Use ggplot() to set the x-axis to States, the y-axis to the margin of victory, and color the bars according to the candidate's dominance.
- Add a bar graph: Draw a bar with geom_bar() to show the gap in support for each candidate. Use fill = Margin > 0 to show a Harris lead in blue and a Trump lead in red.
- Add a margin of error: Add a polling margin of error for each contested state with geom_errorbar() to reflect the volatility of the actual results.
- Color settings: Use scale_fill_manual() to specify the dominant color for Trump and Harris.
- Add labels and titles: Set graph titles and axis labels with labs() to improve understanding.
- Theme settings: set a clean background with theme_minimal(), and adjust the legend position to the top.
Why polls can't be trusted: lessons from historical data
Polls are an important source of information, but they can be quite different from the actual outcome of an election. The 2016 and 2020 U.S. presidential elections are prime examples. In the 2016 election, polls mostly predicted a Hillary Clinton victory, but Donald Trump ended up winning. The margin of error was particularly large in the battleground states, where polls under-represented Trump's core supporters (such as white workers with less education), giving him more support than expected.
The same happened in 2020, when the predictions that Joe Biden would win were correct, but the margin of victory in the toss-up states was much narrower than expected, and there was talk of the phenomenon of "Shy Trump" (Trump supporters who don't show up in polls). Polls are, after all, based on samples, and there is a risk that certain groups are under- or over-represented.
Also, as polling methods have shifted from phone to online, there can be differences in response rates and reliability based on how people take the survey, especially as people with strong political leanings are more likely to participate, which can lead to underrepresentation of neutral voters.
With the possibility of a repeat of this error in the 2024 election, it's important to look at US presidential approval ratings as a trend, not just a number.
Wrapping up: Who will win? Polls don't tell us!
In conclusion, the 2024 US presidential election is expected to be one of the closest in history. Polls give us some interesting hints, but there are many more variables to consider. Hopefully, this post has given you a better understanding of the US presidential race, and we'll be watching the results closely!

