Reputation: 458
I have a different time series for example 4. In this case, I simulated 4 white noise time series.
timeSeries <- matrix(nrow=500,ncol=4)
for(i in 1:4){
timeSeries[,i] <- rnorm(n = 500,mean = 0,sd = i)
}
timeSeries <- as.data.frame(timeSeries)
timeSeries <- cbind(timeSeries,time = 1:500)
Each time series has a different standard deviation. I want to visualize all the time series using a line plot.
plot1 <- ggplot(timeSeries,aes(x = time,y= V1)) + geom_line()
plot2 <- ggplot(timeSeries,aes(x = time,y= V2)) + geom_line()
plot3 <- ggplot(timeSeries,aes(x = time,y= V3)) + geom_line()
plot4 <- ggplot(timeSeries,aes(x = time,y= V4)) + geom_line()
library(gridExtra)
grid.arrange(plot1, plot2,
plot3,plot4,
nrow = 2, ncol=2)
This is the results at the moment:
If I do how I'm doing now, the range is different between the plots. Can I use something like facet_wrap for doing this plot and have the same range of value between the plots?
Upvotes: 1
Views: 146
Reputation: 419
As i understand your question, you want the y-axis to have the same range for the four plots. The simple but maybe not the most efficient way is to add:
ylim(10,-10) #Add this to each plot like following example:
plot1 <- ggplot(timeSeries,aes(x = time,y= V1)) + geom_line() + ylim(10, -10)
Upvotes: 0
Reputation: 50718
Since you mention facet_wrap
, perhaps something like this?
library(tidyverse)
timeSeries %>%
gather(key, val, -time) %>%
ggplot(aes(time, val)) +
geom_line() +
facet_wrap(~ key)
You can specify the behaviour the scales through the scales
parameter in facet_wrap
. The default are fixed (i.e. the same) scales for both axes across all facets.
Upvotes: 2
Reputation: 1089
What about this approach?
ggplot(timeSeries %>% gather(key,value,-time)) +
geom_line(aes(x=time,y=value))+
facet_wrap(~key)
Upvotes: 2