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,130 points
1,537 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,080 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
51 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,750 points
53 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
• 32,440 points
42 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,560 points
114 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,130 points
303 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 Sahiti
• 6,290 points
314 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,290 points
188 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,130 points
768 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,080 points
68 views