1

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

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Does this answer your question? [ggplot with 2 y axes on each side and different scales](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales) – pietrodito May 07 '21 at 07:39
  • 1
    actually, if you want to plot 2 variables on one axis, there can be 2 possibilities. Either plot it on same Y axis (and this case you'll have to transform your data, as suggested by Anoushiravan, now deleted). Else you have to plot it on secondary Y axis, as suggested in above comment – AnilGoyal May 07 '21 at 07:44

1 Answers1

2

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

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45