Ranji Raj
Ranji Raj

Reputation: 818

Horizontal barplot in R

I am trying to reproduce a horizontal barplot (as below), using the unvotes data for the number of votings flagged to be "important" for each issue category. The issues are shown sorted by frequency. I tried the following but stuck at the plot how to generate by ggplot

barhplot

My approach so far:

library(dplyr)
library(unvotes)
library(ggplot2)

unique(issues$issue)
issues1 = issues %>% left_join(roll_calls)
issues1 %>%
  mutate(date2 = ymd(date)) %>%
  mutate(year = year(date2)) -> issues1
head(issues1)

Output of dput

> dput(head(issues))
structure(list(rcid = c(77, 9001, 9002, 9003, 9004, 9005), short_name = c("me", 
"me", "me", "me", "me", "me"), issue = c("Palestinian conflict", 
"Palestinian conflict", "Palestinian conflict", "Palestinian conflict", 
"Palestinian conflict", "Palestinian conflict")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 1

Views: 809

Answers (1)

Till
Till

Reputation: 6628

library(tidyverse)
library(unvotes)
#> If you use data from the unvotes package, please cite the following:
#> 
#> Erik Voeten "Data and Analyses of Voting in the UN General Assembly" Routledge Handbook of International Organization, edited by Bob Reinalda (published May 27, 2013)

roll_calls_imp <- 
  un_roll_call_issues %>% 
  left_join(un_roll_calls) %>% 
  filter(importantvote == 1)
#> Joining, by = "rcid"

roll_calls_imp %>%
  count(issue) %>% # since we want to order the bars by frequency we count the values of `issue` ourselves instead of letting ggplot do the counting
  ggplot(aes(reorder(issue, n) , n)) + # the reorder command sorts the bars by frequency
  geom_col() +
  coord_flip()

Upvotes: 5

Related Questions