Plot times series multiple line x axis as date
I know it might be an old repeated question but I searched all the possible pages and could not find an appropriate answer. I have a time series data like below. I want to plot it as x-axis as time (just year or both month and year) and the rest of the columns as y (all in one plot). I tried different functions such as plot, ggplot(2), ts.plot, plot.ts and none of them gives me what I need. Any suggestions?
r date plot time-series
add a comment |
I know it might be an old repeated question but I searched all the possible pages and could not find an appropriate answer. I have a time series data like below. I want to plot it as x-axis as time (just year or both month and year) and the rest of the columns as y (all in one plot). I tried different functions such as plot, ggplot(2), ts.plot, plot.ts and none of them gives me what I need. Any suggestions?
r date plot time-series
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
Nov 20 at 8:22
add a comment |
I know it might be an old repeated question but I searched all the possible pages and could not find an appropriate answer. I have a time series data like below. I want to plot it as x-axis as time (just year or both month and year) and the rest of the columns as y (all in one plot). I tried different functions such as plot, ggplot(2), ts.plot, plot.ts and none of them gives me what I need. Any suggestions?
r date plot time-series
I know it might be an old repeated question but I searched all the possible pages and could not find an appropriate answer. I have a time series data like below. I want to plot it as x-axis as time (just year or both month and year) and the rest of the columns as y (all in one plot). I tried different functions such as plot, ggplot(2), ts.plot, plot.ts and none of them gives me what I need. Any suggestions?
r date plot time-series
r date plot time-series
edited Nov 20 at 5:31
asked Nov 20 at 5:21
Heerj
135
135
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
Nov 20 at 8:22
add a comment |
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
Nov 20 at 8:22
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use
str()
, head()
or screenshot)? You can use the reprex
and datapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?– Tung
Nov 20 at 8:22
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use
str()
, head()
or screenshot)? You can use the reprex
and datapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?– Tung
Nov 20 at 8:22
add a comment |
2 Answers
2
active
oldest
votes
Try this.
you need to create a date field. See the sample below, it has some dummy values code which you can ignore. You can use the date part (I use lubridate), and ggplot
library(tidyverse)
library(lubridate)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp$Uymax <- rnorm(48,4555,54)
mp$Uymin<- rnorm(48,5656,34)
mp$uymedian<- rnorm(48,6767,43)
mp$uy90<- rnorm(48,7676,56)
mp$uy10<- rnorm(48,7676,66)
library(tidyverse)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp %>% ggplot() +
geom_line(aes(date,Uymax,color='Uymax')) +
geom_line(aes(date,Uymin,color='Uymin')) +
geom_line(aes(date,uymedian,color='uymedian')) +
geom_line(aes(date,uy90,color='uy90')) +
geom_line(aes(date,uy10,color='uy10'))
Thanks @Aji. It worked. I also did a minor change to it. Since in your code, the y lable is set autumatically based on the first line, Uymax (which might not what desired), I chaned the first line to ggplot(mp, aes(Date,mp)). So, the y lab is now mp or can be changed to any other desired name.
– Heerj
Nov 20 at 23:20
add a comment |
First, I would suggest to transform your data from wide-format to long-format in order to graph multiple variables in one plot. Here's a good tutorial that would help you with that.
Here's an example that mimics your code
library(reshape2); library(ggplot2)
df <- data.frame(Month = 1:11, Year = 2000: 2010, UY_Min = 1:11, UY_Media = 20:30, UY_90Per = 30:40)
df_long <- melt(df, id.vars = c("Month", "Year"), variable.name = "UY", value.name =
"Values") #convert the table from wide to long format. you can name variable.name and value.name appropriately
here, I'm using a line plot as an example, but really once you shape your data to long-format, you can use any geom function you desire. then dress it up as necessary.
ggplot(df_long, aes(Year, The_Values, col = UY)) +
geom_line() +
scale_x_continuous(breaks = seq(2000, 2010, by = 1))
@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.
– Heerj
Nov 20 at 23:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386675%2fplot-times-series-multiple-line-x-axis-as-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this.
you need to create a date field. See the sample below, it has some dummy values code which you can ignore. You can use the date part (I use lubridate), and ggplot
library(tidyverse)
library(lubridate)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp$Uymax <- rnorm(48,4555,54)
mp$Uymin<- rnorm(48,5656,34)
mp$uymedian<- rnorm(48,6767,43)
mp$uy90<- rnorm(48,7676,56)
mp$uy10<- rnorm(48,7676,66)
library(tidyverse)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp %>% ggplot() +
geom_line(aes(date,Uymax,color='Uymax')) +
geom_line(aes(date,Uymin,color='Uymin')) +
geom_line(aes(date,uymedian,color='uymedian')) +
geom_line(aes(date,uy90,color='uy90')) +
geom_line(aes(date,uy10,color='uy10'))
Thanks @Aji. It worked. I also did a minor change to it. Since in your code, the y lable is set autumatically based on the first line, Uymax (which might not what desired), I chaned the first line to ggplot(mp, aes(Date,mp)). So, the y lab is now mp or can be changed to any other desired name.
– Heerj
Nov 20 at 23:20
add a comment |
Try this.
you need to create a date field. See the sample below, it has some dummy values code which you can ignore. You can use the date part (I use lubridate), and ggplot
library(tidyverse)
library(lubridate)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp$Uymax <- rnorm(48,4555,54)
mp$Uymin<- rnorm(48,5656,34)
mp$uymedian<- rnorm(48,6767,43)
mp$uy90<- rnorm(48,7676,56)
mp$uy10<- rnorm(48,7676,66)
library(tidyverse)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp %>% ggplot() +
geom_line(aes(date,Uymax,color='Uymax')) +
geom_line(aes(date,Uymin,color='Uymin')) +
geom_line(aes(date,uymedian,color='uymedian')) +
geom_line(aes(date,uy90,color='uy90')) +
geom_line(aes(date,uy10,color='uy10'))
Thanks @Aji. It worked. I also did a minor change to it. Since in your code, the y lable is set autumatically based on the first line, Uymax (which might not what desired), I chaned the first line to ggplot(mp, aes(Date,mp)). So, the y lab is now mp or can be changed to any other desired name.
– Heerj
Nov 20 at 23:20
add a comment |
Try this.
you need to create a date field. See the sample below, it has some dummy values code which you can ignore. You can use the date part (I use lubridate), and ggplot
library(tidyverse)
library(lubridate)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp$Uymax <- rnorm(48,4555,54)
mp$Uymin<- rnorm(48,5656,34)
mp$uymedian<- rnorm(48,6767,43)
mp$uy90<- rnorm(48,7676,56)
mp$uy10<- rnorm(48,7676,66)
library(tidyverse)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp %>% ggplot() +
geom_line(aes(date,Uymax,color='Uymax')) +
geom_line(aes(date,Uymin,color='Uymin')) +
geom_line(aes(date,uymedian,color='uymedian')) +
geom_line(aes(date,uy90,color='uy90')) +
geom_line(aes(date,uy10,color='uy10'))
Try this.
you need to create a date field. See the sample below, it has some dummy values code which you can ignore. You can use the date part (I use lubridate), and ggplot
library(tidyverse)
library(lubridate)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp$Uymax <- rnorm(48,4555,54)
mp$Uymin<- rnorm(48,5656,34)
mp$uymedian<- rnorm(48,6767,43)
mp$uy90<- rnorm(48,7676,56)
mp$uy10<- rnorm(48,7676,66)
library(tidyverse)
mp$date <- ymd(paste(mp$year,'-',mp$month,'-','01',sep = ""))
mp %>% ggplot() +
geom_line(aes(date,Uymax,color='Uymax')) +
geom_line(aes(date,Uymin,color='Uymin')) +
geom_line(aes(date,uymedian,color='uymedian')) +
geom_line(aes(date,uy90,color='uy90')) +
geom_line(aes(date,uy10,color='uy10'))
answered Nov 20 at 6:00
Aji
12613
12613
Thanks @Aji. It worked. I also did a minor change to it. Since in your code, the y lable is set autumatically based on the first line, Uymax (which might not what desired), I chaned the first line to ggplot(mp, aes(Date,mp)). So, the y lab is now mp or can be changed to any other desired name.
– Heerj
Nov 20 at 23:20
add a comment |
Thanks @Aji. It worked. I also did a minor change to it. Since in your code, the y lable is set autumatically based on the first line, Uymax (which might not what desired), I chaned the first line to ggplot(mp, aes(Date,mp)). So, the y lab is now mp or can be changed to any other desired name.
– Heerj
Nov 20 at 23:20
Thanks @Aji. It worked. I also did a minor change to it. Since in your code, the y lable is set autumatically based on the first line, Uymax (which might not what desired), I chaned the first line to ggplot(mp, aes(Date,mp)). So, the y lab is now mp or can be changed to any other desired name.
– Heerj
Nov 20 at 23:20
Thanks @Aji. It worked. I also did a minor change to it. Since in your code, the y lable is set autumatically based on the first line, Uymax (which might not what desired), I chaned the first line to ggplot(mp, aes(Date,mp)). So, the y lab is now mp or can be changed to any other desired name.
– Heerj
Nov 20 at 23:20
add a comment |
First, I would suggest to transform your data from wide-format to long-format in order to graph multiple variables in one plot. Here's a good tutorial that would help you with that.
Here's an example that mimics your code
library(reshape2); library(ggplot2)
df <- data.frame(Month = 1:11, Year = 2000: 2010, UY_Min = 1:11, UY_Media = 20:30, UY_90Per = 30:40)
df_long <- melt(df, id.vars = c("Month", "Year"), variable.name = "UY", value.name =
"Values") #convert the table from wide to long format. you can name variable.name and value.name appropriately
here, I'm using a line plot as an example, but really once you shape your data to long-format, you can use any geom function you desire. then dress it up as necessary.
ggplot(df_long, aes(Year, The_Values, col = UY)) +
geom_line() +
scale_x_continuous(breaks = seq(2000, 2010, by = 1))
@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.
– Heerj
Nov 20 at 23:03
add a comment |
First, I would suggest to transform your data from wide-format to long-format in order to graph multiple variables in one plot. Here's a good tutorial that would help you with that.
Here's an example that mimics your code
library(reshape2); library(ggplot2)
df <- data.frame(Month = 1:11, Year = 2000: 2010, UY_Min = 1:11, UY_Media = 20:30, UY_90Per = 30:40)
df_long <- melt(df, id.vars = c("Month", "Year"), variable.name = "UY", value.name =
"Values") #convert the table from wide to long format. you can name variable.name and value.name appropriately
here, I'm using a line plot as an example, but really once you shape your data to long-format, you can use any geom function you desire. then dress it up as necessary.
ggplot(df_long, aes(Year, The_Values, col = UY)) +
geom_line() +
scale_x_continuous(breaks = seq(2000, 2010, by = 1))
@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.
– Heerj
Nov 20 at 23:03
add a comment |
First, I would suggest to transform your data from wide-format to long-format in order to graph multiple variables in one plot. Here's a good tutorial that would help you with that.
Here's an example that mimics your code
library(reshape2); library(ggplot2)
df <- data.frame(Month = 1:11, Year = 2000: 2010, UY_Min = 1:11, UY_Media = 20:30, UY_90Per = 30:40)
df_long <- melt(df, id.vars = c("Month", "Year"), variable.name = "UY", value.name =
"Values") #convert the table from wide to long format. you can name variable.name and value.name appropriately
here, I'm using a line plot as an example, but really once you shape your data to long-format, you can use any geom function you desire. then dress it up as necessary.
ggplot(df_long, aes(Year, The_Values, col = UY)) +
geom_line() +
scale_x_continuous(breaks = seq(2000, 2010, by = 1))
First, I would suggest to transform your data from wide-format to long-format in order to graph multiple variables in one plot. Here's a good tutorial that would help you with that.
Here's an example that mimics your code
library(reshape2); library(ggplot2)
df <- data.frame(Month = 1:11, Year = 2000: 2010, UY_Min = 1:11, UY_Media = 20:30, UY_90Per = 30:40)
df_long <- melt(df, id.vars = c("Month", "Year"), variable.name = "UY", value.name =
"Values") #convert the table from wide to long format. you can name variable.name and value.name appropriately
here, I'm using a line plot as an example, but really once you shape your data to long-format, you can use any geom function you desire. then dress it up as necessary.
ggplot(df_long, aes(Year, The_Values, col = UY)) +
geom_line() +
scale_x_continuous(breaks = seq(2000, 2010, by = 1))
answered Nov 20 at 6:25
Wally Ali
2,147817
2,147817
@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.
– Heerj
Nov 20 at 23:03
add a comment |
@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.
– Heerj
Nov 20 at 23:03
@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.
– Heerj
Nov 20 at 23:03
@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.
– Heerj
Nov 20 at 23:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386675%2fplot-times-series-multiple-line-x-axis-as-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use
str()
,head()
or screenshot)? You can use thereprex
anddatapasta
packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?– Tung
Nov 20 at 8:22