Time series stacked line plots with different time intereval

46 views
Skip to first unread message

amanjot.cvc

unread,
Mar 27, 2014, 9:06:31 AM3/27/14
to ggp...@googlegroups.com
Hi,
I am trying to plot a stacked line plot that has five variables using "facets" command. I have been able to stack the plots; however, since the time step for each variable is not the same, the ones that are at lower resolution have only dots, no lines. Is there a way to stack the plots using same time scale (x-axis); however, still plotting lines or even bars? I have attached the plot for your reference. What I am intending to accomplish is to have lines at the bottom three plots. The time step for upper three plots is month and for lower three is year.


Ista Zahn

unread,
Mar 27, 2014, 9:47:11 AM3/27/14
to amanjot.cvc, ggplot2
Better to show the code rather than the plot. I can't replicate your problem:

library(ggplot2)
library(reshape2)
dat <- data.frame(x = as.Date(c(1:10, c(2, 4, 6, 8, 10))), y = rnorm(15), z = c(rep("a", 10), rep("b", 5)))

ggplot(dat, aes(x = x, y = y, color = z)) +
    geom_line() +
    facet_wrap(~z, ncol = 1)

produces 

Inline image 1



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

amanjot.cvc

unread,
Mar 27, 2014, 3:35:35 PM3/27/14
to ggp...@googlegroups.com, amanjot.cvc
Below is the code Ista and attached is the part of the dataset

try = read.csv(file="Data.csv",head=TRUE,sep=",")
try$Date = as.Date(try$Date,format='%d/%m/%Y') #IMP: use Y not y for year
head(try)
library(reshape2)
try1=melt(try, id=c("Date"))
library(ggplot2)
try1plot=qplot(Date, value, data=try1, colour = variable) + geom_line()#,facets=variable)
try1plot + facet_grid(variable ~ ., scales = "free")
Data.csv

Ista Zahn

unread,
Mar 27, 2014, 5:14:07 PM3/27/14
to amanjot. cvc, ggplot2

I'm not at a computer now, but try removing missing values before plotting.

Dennis Murphy

unread,
Mar 27, 2014, 8:42:41 PM3/27/14
to amanjot.cvc, ggplot2
I'll try to pinch hit for Ista since he appears to be busy at the moment. His advice about removing NAs in Vars 4-6 is correct, as usual; here's one way to do it. Call this the split-reshape-combine approach :)


library(reshape2)
library(ggplot2)

# try() is the name of a common function in R, so rename
Try = read.csv(file="Data.csv", header = TRUE, sep = ",")
Try$Date = as.Date(Try$Date, format='%d/%m/%Y')

# Create two sub-data frames: one for the first three variables,
# another for the latter three, both including Date
try0 <- Try[, 1:4]
try1 <- Try[, c(1, 5:7)]

# logical vector to determine if any of Var4-Var6 are nonzero
# in a given row of try1; use this for subsetting below
v <- apply(try1[, -1], 1, function(x) any(!is.na(x)))

trym0 <- melt(try0, id = "Date")     # 738 x 3
trym1 <- melt(try1[v, ], id = "Date")    # 45 x 3

# Note: variable has different levels in trym0 and trym1

# The two data frames have the same variables, so rbind them
trym <- rbind(trym0, trym1)
# redefine the factor 'variable' to get all of the levels from both
trym$variable <- factor(trym$variable)

# Now plot...
qplot(Date, value, data=trym, colour = variable, size = I(1.2)) + geom_line() +
    facet_grid(variable ~ ., scales = "free")

Observe that you still get a gap in the line plot of Var4, but that's because it has missing values for three years where Vars 5 and 6 do not. The gap is informative. You see the same thing in Var1, but it's less obvious due to the data density. I reduced the point size to reduce overlap of the points - if you like the original point size, just get rid of the argument in qplot(). (size = 1.3 will give you visually larger points).

Dennis

amanjot.cvc

unread,
Mar 28, 2014, 11:45:30 AM3/28/14
to ggp...@googlegroups.com, amanjot.cvc
Many thanks Dennis for an elaborated reply. I believe you sensed that I am new to r and ggplot. Infact, this is my first plot.

I also tried using 
try=melt(try, na.rm = TRUE, id=c("date")) 
to get rid of NAs and it worked fine; however, the same observation as you suggested, I also lost some valuable data when there was no sampling done. 

Anyways, your code works as I wanted. 

Dennis, is there a way to mix bar plots with lines and to alternate labels and values to right and left. For example first graph has values on left and label on right reverse for second and so on. I have seen something similar somewhere.

Cheers,

Amanjot

Amanjot Singh, Ph.D., P.Eng.
Reply all
Reply to author
Forward
0 new messages