0

I am working with a Supreme Court dataset. I am trying to visualize the percentage of cases in each term that were decided by a one-vote margin. I have variables for terms, the number of votes in the majority, and the number of votes in the minority. The best option for visualization is a line graph with terms as the x-axis and the percentage of decisions made with a one-vote margin as the y-axis.

library(tidyverse)
library(dplyr)
scdby %>%
  select(majVotes, minVotes, term)

I created a new variable for the vote margin.

scdbv %>%
 select(majVotes, minVotes, term) %>%
 mutate(margin = majVotes - minVotes)

Since then, I feel like I have tried every method under the sun to get the percent of single margin votes to apply in ggplot for my graph. This is the most recent method:

scdbv %>%
  select(majVotes, minVotes, term) %>%
  mutate(margin = majVotes - minVotes) %>%
  mutate(margin1 = if_else(margin == "1", "1", "NA")) %>%
  mutate(margin1 = as.integer(margin1)) %>%
  ggplot(aes(x = term)) +
  geom_line(aes(y = count(margin1) / n()))

Which returns:

  no applicable method for 'groups' applied to an object of class "c('integer', 'numeric')"

I am sure there is something very simple I am missing with how to do this.

LaurenG
  • 31
  • 1
  • 1
    Not a very important point, but if you load the tidyverse, you do not need to also load dplyr since dplyr is part of the core tidyverse packages (so it gets loaded when you load the tidyverse package) – prosoitos Oct 22 '18 at 04:40
  • Your `x` and `y` should be in the same `aes()`. Maybe read this to get started with using ggplot2: http://r4ds.had.co.nz/data-visualisation.html – prosoitos Oct 22 '18 at 04:45
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 22 '18 at 04:51

1 Answers1

1
scdbv %>%
  select(majVotes, minVotes, term) %>%
  mutate(margin = majVotes - minVotes) %>%
  group_by(term) %>%
  summarize(percentage=sum(margin==1)/n()) %>%
  ggplot(aes(term,percentage)) +
    geom_line()

After creating margin, I group by term, and create a data.frame where for each term, the percentage of cases with a margin of one is summarized. I then plot this new data. As noted in a comment above, your x and y are best defined already in the ggplot() call, and then geom_line can contain only stuff like color and line weight.

For example, I created this fake sample:

scdbv<-data.frame(majVotes=c(6,4,5,6,4,5),minVotes=c(5,2,3,3,3,4), term=c(1,1,2,2,3,3))

and the result is (the somewhat uninspiring):

enter image description here

You might want to look into theme() to make this more appealing.

iod
  • 7,412
  • 2
  • 17
  • 36
  • Thank you so much! This was exactly what I needed. – LaurenG Oct 22 '18 at 05:02
  • Glad I could help. Don't forget to upvote and accept! https://stackoverflow.com/help/someone-answers (or just accept, since you don't have the upvote privilege yet -- but accepting gains your reputation!) – iod Oct 22 '18 at 05:05