Plot times series multiple line x axis as date












0















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?



enter image description here










share|improve this question

























  • 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 '18 at 8:22
















0















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?



enter image description here










share|improve this question

























  • 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 '18 at 8:22














0












0








0








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?



enter image description here










share|improve this question
















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?



enter image description here







r date plot time-series






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 5:31







Heerj

















asked Nov 20 '18 at 5:21









HeerjHeerj

135




135













  • 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 '18 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 '18 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 '18 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 '18 at 8:22












2 Answers
2






active

oldest

votes


















0














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'))





share|improve this answer
























  • 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 '18 at 23:20



















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))


enter image description here






share|improve this answer
























  • @Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.

    – Heerj
    Nov 20 '18 at 23:03











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
});


}
});














draft saved

draft discarded


















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









0














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'))





share|improve this answer
























  • 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 '18 at 23:20
















0














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'))





share|improve this answer
























  • 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 '18 at 23:20














0












0








0







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'))





share|improve this answer













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'))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 6:00









AjiAji

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 '18 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 '18 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 '18 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 '18 at 23:20













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))


enter image description here






share|improve this answer
























  • @Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.

    – Heerj
    Nov 20 '18 at 23:03
















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))


enter image description here






share|improve this answer
























  • @Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.

    – Heerj
    Nov 20 '18 at 23:03














1












1








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))


enter image description here






share|improve this answer













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))


enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 6:25









Wally AliWally Ali

2,152917




2,152917













  • @Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.

    – Heerj
    Nov 20 '18 at 23:03



















  • @Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.

    – Heerj
    Nov 20 '18 at 23:03

















@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.

– Heerj
Nov 20 '18 at 23:03





@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need.

– Heerj
Nov 20 '18 at 23:03


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?