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?
Asked
Active
Viewed 1,847 times
0
-
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`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 20 '18 at 08:22
2 Answers
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))

Wally Ali
- 2,500
- 1
- 13
- 20
-
@Thanks Wally Ali. I had tried this before. The graph is not as appropriate as I need. – Heerbod Nov 20 '18 at 23:03
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'))

Aji
- 133
- 1
- 5
-
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. – Heerbod Nov 20 '18 at 23:20