Is there a way to plot 3 different variables (time and two dependent variables)?

320 Views Asked by At

I am doing a project for my class in R Analysis but I need two univariate graphs and one bivariate graph. Currently, my dataset is comprised of 5 columns (see sample line below). I have done one graph with x=time and y=stringency_index, one with x=time and y=new_cases_smoothed, and I attempted a graph plotting stringency index against new cases but it did not turn out looking like a real graph. Is there a way to graph these variables against each other to see if there's a pattern over time?

I'll attach my dput and a photo of one of the graphs I made.

   Entity, Code,  Day, stringency_index, new_cases_smoothed 
1   China   CHN 2020-03-14  81.02   29.571
2   China   CHN 2020-03-15  81.02   25.714
3   China   CHN 2020-03-16  79.17   24.714
4   China   CHN 2020-03-17  79.17   24.429
5   China   CHN 2020-03-18  79.17   25.857
6   China   CHN 2020-03-19  79.17   32.000

[enter image description here][1]dput(head(joined))
structure(list(Entity = c("China", "China", "China", "China", 
"China", "China"), Code = c("CHN", "CHN", "CHN", "CHN", "CHN", 
"CHN"), Day = structure(c(18335, 18336, 18337, 18338, 18339, 
18340), class = "Date"), stringency_index = c(81.02, 81.02, 79.17, 
79.17, 79.17, 79.17), new_cases_smoothed = c(29.571, 25.714, 
24.714, 24.429, 25.857, 32)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

Here is one of the graphs I made

1

There are 1 best solutions below

0
On

Do you want this?

library(tidyverse)
df %>%
  pivot_longer(c(stringency_index, new_cases_smoothed), 
               names_to = "Vars", values_to = "Values") %>%
  ggplot() + 
  geom_line(aes(x = Day, y = Values, color = Vars)) +
  geom_point(aes(x = Day, y = Values, color = Vars))

enter image description here