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,210 points
2,184 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
85 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
106 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,490 points
70 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 in Data Analytics by MD
• 57,020 points
102 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,210 points
619 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,320 points
563 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,320 points
273 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,210 points
995 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
111 views