Plot two variables as lines on the same graph using ggplot

0 votes

Consider a data like this:

data <-
  data.frame(
    variable0 = 100 + c(0, cumsum(runif(49, -20, 20))),
    variable1 = 150 + c(0, cumsum(runif(49, -10, 10))),
    date = seq(as.Date("2007-01-01"), by="1 month", length.out=100)
  )

How to plot time series for variable0 and variable1 on the same graph with date on x-axis.

I want to use ggplot2.

Can someone please help?

Apr 17, 2018 in Data Analytics by DataKing99
• 8,250 points
5,067 views

1 answer to this question.

0 votes

If you have small number of variables, then you use build the plot manually

ggplot(data, aes(date)) + 
  geom_line(aes(y = variable0, colour = "variable0")) + 
  geom_line(aes(y = variable1, colour = "variable1"))
answered Apr 17, 2018 by kappa3010
• 2,090 points

Related Questions In Data Analytics

0 votes
0 answers

Plot different lines as per category and color using ggplot

Hi, how to show different types of ...READ MORE

Jul 23, 2019 in Data Analytics by nidhi
751 views
0 votes
1 answer

How can I print string and variable contents on the same line using R?

There are two options for doing so.  You ...READ MORE

answered May 9, 2018 in Data Analytics by zombie
• 3,790 points
1,885 views
0 votes
1 answer

How to edit the labels and limit if a plot using ggplot? - R

Add a limit to axis ticks using ...READ MORE

answered Nov 3, 2019 in Data Analytics by anonymous
• 33,030 points
721 views
+3 votes
1 answer

R plot for train and test data on same graph

Hi, You can use the subplot function. It ...READ MORE

answered Jul 27, 2020 in Data Analytics by MD
• 95,460 points
3,283 views
0 votes
1 answer

Working of ggplot scale_continuous 'expand' argument

According to the document if you set limits manually, ...READ MORE

answered Apr 13, 2018 in Data Analytics by DataKing99
• 8,250 points
3,417 views
0 votes
1 answer

Save a plot as image on the disk using R

Consider for both the situations: 1. Image will ...READ MORE

answered Apr 14, 2018 in Data Analytics by Sahiti
• 6,370 points
2,159 views
0 votes
1 answer

R lag irregular time series data

You could try using: library(dplyr) library(zoo) na.locf(ts$value[sapply(ts$time, function(x) min(which(ts$time - ...READ MORE

answered May 11, 2018 in Data Analytics by Sahiti
• 6,370 points
1,041 views
0 votes
1 answer

How to forecast season and trend of data using STL and ARIMA in R?

You can use the forecast.stl function for the ...READ MORE

answered May 19, 2018 in Data Analytics by DataKing99
• 8,250 points
2,188 views
0 votes
1 answer
0 votes
1 answer

How can I use parallel so that it preserves the list of data frames

You can use pmap as follows: nc <- ...READ MORE

answered Apr 4, 2018 in Data Analytics by kappa3010
• 2,090 points
1,091 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP