3

I'm trying to compare obama's first year approval ratings with Trump's first year approval ratings in a line chart.

The trouble here is that I use two different years obama (2009) and Trump (2017). Can't seem to figure out how to solve this...

rating_comparison %>%
  ggplot(aes(endDate, rating, group = 1)) +
  geom_line(data = filter(rating_comparison, 
                          endDate > "2017-01-28",
                          type == "Overall.A")) +
  geom_line(data = filter(rating_comparison, 
                          president == "Obama" &
                            endDate > "2009-01-25" &
                            endDate < "2010-01-24",
                          type == "Overall.A")) 

enter image description here

3
  • 2
    You'll need to convert the dates into something comparable, like day or week number since taking office. It would help to see the data rating_comparison. Commented Dec 13, 2017 at 22:09
  • Yes that might work. It's quite a large dataset... Commented Dec 13, 2017 at 22:23
  • Another strategy would be to add 8 years to all of the Obama dates, so both sets of data would have 2017 for year and plot on the same x values, then set the axis labels to show only month and day, not year + scale_x_date(date_labels = "%b %d") Commented Dec 13, 2017 at 22:57

1 Answer 1

3

I will assume from your code that the data looks something like this:

president   startDate    endDate  rating       type
    Obama  2017-01-17 2017-01-19      59  Overall.A
    Trump  2017-01-25 2017-01-27      42  Overall.A

I also assume that the date columns are of type Date. If not, convert them using something like:

rating_comparison$endDate <- as.Date(rating_comparison$endDate, "%Y-%m-%d")

One approach is to convert each date to "days since the first date", by president. Then plot rating versus day. Of course, this assumes that the "first dates" are somehow comparable - I assume they equate to the first polling date of each presidency.

For example, to colour by president, restricting to the first 365 days:

library(dplyr)
library(ggplot2)
rating_comparison %>% 
  filter(type == "Overall.A") %>%
  group_by(president) %>% 
  mutate(Day = as.numeric(endDate - min(endDate))) %>% 
  ungroup() %>% 
  filter(Day <= 365) %>% 
  ggplot(aes(Day, rating)) + 
    geom_line(aes(color = president))

I grabbed some Gallup data from the web, similar but not identical to yours, to get this result:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you comprehensive answer. obama dates are not from 2017 rather in 2009 of course.
Sure. The trick is to make the year irrelevant, by making all dates relative to the first date, for each president.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.