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,100 points
634 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,010 points

Related Questions In Data Analytics

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,690 points
12 views
0 votes
1 answer

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
29 views
0 votes
1 answer

Error saying "'names' attribute [27] must be the same length as the vector [21]"

When you end up with this error ...READ MORE

answered Nov 12, 2018 in Data Analytics by Maverick
• 10,000 points
82 views
0 votes
1 answer

?: The only 'Run as' choice 'Run Configurations. Why?

Dear Learner, Greetings ! The issue which you are ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
12 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 12, 2018 in Data Analytics by DataKing99
• 8,100 points
44 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 13, 2018 in Data Analytics by darklord
• 6,140 points
44 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 darklord
• 6,140 points
67 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 18, 2018 in Data Analytics by DataKing99
• 8,100 points
366 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,010 points
18 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.