Data visualization iii

Answers for Quiz 9

Load Libraries

Question 1

Create a bar chart that shows the average hours Americans spend on five activities by year. Use the timeline argument to create an animation that will animate through the years.

e_charts-1

Start with spend_time

spend_time <- read_csv("spend_time.csv")
spend_time %>% 
  group_by(year) %>% 
  e_charts(x = activity, timeline = TRUE) %>% 
  e_timeline_opts(autoplay = TRUE) %>% 
  e_bar(serie = avg_hours) %>% 
  e_title(text = 'Average Hours Americans Spend per day on Each Activity') %>% 
  e_legend(show = FALSE)

Question 2

echarts-2

Create a line chart for the activities that American spend time on.

Start with spend_time

spend_time %>% 
  mutate(year=paste(year, "12", "31", sep = "-")) %>% 
  mutate(year = lubridate::ymd(year)) %>% 
  group_by(activity) %>% 
  e_charts(x = year) %>% 
  e_line(serie = avg_hours) %>% 
  e_tooltip() %>% 
  e_title(text =  'Average Hours Americans Spend per day on Each Activity') %>% 
  e_legend(top = 40)

Question 3

Modify slide 82

ggplot(spend_time, aes(x = year, y = avg_hours, color = activity)) +
  geom_point() +
  geom_mark_ellipse(aes(filter = activity == "leisure/sports",
                        description = "Americans spend on average more time each day on leisure/sports than the other activities"))

Question 4

Modify the tidyquant example in the video

Retrieve stock price for Facebook, ticker: FB, using tq_get

df <- tq_get("FB", get = "stock.prices",
             from = "2019-08-01", to = "2020-07-28")

Create a plot with the df data

df %>% 
ggplot(aes(x = date, y = close)) +
  geom_line() +
  geom_mark_ellipse(aes(
    filter = date == "2020-01-03",
    description = "CDC Director Robert Redfield was notified by a counterpart in China that a 'mysterious respiratory illness was spreading in Wuhan [China]'; he notified HHS Secretary Alex Azar, who shared the report with the National Security Council (NSC). According to The Washington Post, warnings about the virus were included in the President's Daily Brief in early January, an indicator of the emphasis placed on the virus by the intelligence community."
    ), fill = "yellow") +
  geom_mark_ellipse(aes(
    filter = date == "2020-03-16",
    description = "President Trump issued new guidelines urging people to avoid social gatherings of more than ten people and to restrict discretionary travel. He stopped short of ordering a quarantine or a curfew, but he said restrictions may last until July or August. He acknowledged that the country may be headed for a recession. Despite the fact that the Federal Reserve Bank lowered interest rates the day prior, the stock market fell once again."
    ), fill = "red") +
  labs(
    title = "Facebook",
    
    x = NULL,
    y = "closing price per share",
    caption = "Source: https://en.wikipedia.org/wiki/Timeline_of_the_COVID-19_pandemic_in_the_United_States")