vote results percentage graph in r

To create a vote result percentage graph in R, we can use the ggplot2 library which provides an easy way to create professional-looking graphics. Here are the steps to create this graph:

  1. First, we need to create a data frame with the vote results. In this example, we will create a data frame with two columns: the political parties and the percentage of votes they received.
main.r
vote_results <- data.frame(
   party = c("Party A", "Party B", "Party C"),
   percent_votes = c(40, 30, 30)
)
110 chars
5 lines
  1. Next, we will create a bar graph using ggplot2. We can use geom_bar to create the bars and coord_flip to rotate the graph sideways so that it is easier to read.
main.r
library(ggplot2)

vote_graph <- ggplot(vote_results, aes(x = party, y = percent_votes)) +
  geom_bar(stat = "identity") +
  coord_flip()

vote_graph
149 chars
8 lines
  1. Finally, we can add labels and customize the graph as necessary. For example, we can change the color of the bars using fill, add axis labels using xlab and ylab, and add a title using ggtitle.
main.r
vote_graph <- ggplot(vote_results, aes(x = party, y = percent_votes, fill = party)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  xlab("Political parties") +
  ylab("Percentage of votes") +
  ggtitle("2020 Presidential Elections Results")

vote_graph
258 chars
9 lines

This will create a graph that looks like this:

Vote Results Percentage Graph

gistlibby LogSnag