Plotting 2 Lines on the same graph

Looking to get an idea of how this might work, if I am using ggplot to plot my data, and then I have 2 versions of my graph. V1 is my linear scale and V2 is the same graph however with the scale_y_log10() applied. I'd like to see both lines on the same graph. I've seen online that using ggarrange I can combine both plots however that just shows 2 graphs in one image separately. I am looking to have 2 lines on the same graph. Some guidance into functions I should look into would be great

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

The following 2 graphs are made from the code. I use the function scale_y_log10() to create the different graphs however everything else is the same. How can I have both plots on one graph.

#Read In The Files
install.packages("readxl")

#these libraries are necessary
library(readxl)
library(httr)
library(tidyverse)

#create the URL where the dataset is stored with automatic updates every day
url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")

#download the dataset from the website to a local temporary file
GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))

#read the Dataset sheet into “R”
data <- read_excel(tf)

data %>% 
  filter(countriesAndTerritories %in% c("Canada")) %>% 
  ggplot(aes(dateRep, cases, col = countriesAndTerritories)) + 
  geom_point() +
  #geom_line() +
  scale_y_log10() +
  geom_smooth() +
  theme(legend.position = "bottom") +
  ggtitle("Number of New COVID-19 Cases In Canada") +
  xlab("Date In Months") + 
  ylab("Number of New Cases") +
  theme(legend.position="right") 

Graph 1 - Log Applied

Graph 2 - Linear Scale

Hi - This should get you started. What you'll see is that if you want to use two different variables in the same plot, then you need to put those variables inside of different geoms. I don't understand exactly why; maybe others can help out there! You'll also see that when I logged the variable I had to add +1 because you can't take a log of zero. This probably isn't appropriate here but it let me give you working code!

data %>% 
  filter(countriesAndTerritories %in% c("Canada")) %>% 
  ggplot() + 
  geom_point(aes(dateRep, log(cases+1), col = countriesAndTerritories)) +
  geom_smooth(aes(dateRep, log(cases+1), col = countriesAndTerritories)) +
  geom_point(aes(dateRep, cases, col = countriesAndTerritories)) +
  geom_smooth(aes(dateRep, cases, col = countriesAndTerritories)) +
  scale_y_log10() +
  theme(legend.position = "bottom") +
  ggtitle("Number of New COVID-19 Cases In Canada") +
  xlab("Date In Months") + 
  ylab("Number of New Cases") +
  theme(legend.position="right")

You could stitch the two plots together rather than jumping through hops to use facets:

I could be wrong but I think he wants all the lines in a single graph instead of a multi panel graph/chart. Is that even possible?

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.