Alex Rivera | Logout

Stacking multiple plots, vertically with the same x axis but different Y axes in R

Asked 2012-08-03T11:01:12.330
16

I have a data.frame with multiple time series vectors against a date:time vector. I would like to plot all of the relevant vectors, vertically stacked on separate graphs with the same X axis but unique Y axes. A graph similar to this one: enter image description here

my data looks like this:

 dt <- structure(list(DEPTH = c(156, 156.5, 157.4, 158.15, 158.8, 159.2, 
159.75, 160.35, 160.85, 161.1, 161.6, 162.05, 162.5, 162.65, 
163.15, 163.45, 163.55, 163.8, 163.65, 163.75, 163.8, 163.8, 
163.75, 164.45, 164.8, 165.35, 165.65, 165.75, 166.1, 166.75, 
167, 167.2, 167.65, 168, 168.8, 169.3, 169.7, 170.2, 170.65, 
170.9, 171.45, 171.65, 172, 172.1, 172.25, 173, 173.4, 173.9, 
174.2, 174.6, 175, 175.25, 175.45, 175.9, 176.25, 176.7, 177, 
177.15, 177.5, 178, 178.5, 179.05, 179.2, 180.7, 181.05, 181.25, 
181.5, 181.7, 182.1, 182.3, 182.35, 182.75, 183.1, 183.65, 184.3, 
184.6, 185.1, 185.15, 185.3, 185.15, 185.25, 185.3, 185.15), 
    Smooth.Vert.Speed = c(-0.550000000000011, -0.5, -0.900000000000006, 
    -0.75, -0.650000000000006, -0.399999999999977, -0.550000000000011, 
    -0.599999999999994, -0.5, -0.25, -0.5, -0.450000000000017, 
    -0.449999999999989, -0.150000000000006, -0.5, -0.299999999999983, 
    -0.100000000000023, -0.25, 0.150000000000006, -0.0999999999999943, 
    -0.0500000000000114, 0, 0.0500000000000114, -0.699999999999989, 
    -0.350000000000023, -0.549999999999983, -0.300000000000011, 
    -0.0999999999999943, -0.349999999999994, -0.650000000000006, 
    -0.25, -0.199999999999989, -0.450000000000017, -0.349999999999994, 
    -0.800000000000011, -0.5, -0.399999999999977, -0.5, -0.450000000000017, 
    -0.25, -0.549999999999983, -0.200000000000017, -0.349999999999994, 
    -0.0999999999999943, -0.150000000000006, -0.75, -0.400000000000006, 
    -0.5, -0.299999999999983, -0.400000000000006, -0.400000000000006, 
    -0.25, -0.199999999999989, -0.450000000000017, -0.349999999999
Edit
Report

1 Answer

14

I agree with @PaulHiemstra, ggplot2 is the way to go.

Assuming Smooth.Vert.Speed is the common x-axis variable against which you want to plot DEPTH, X, Y and Z...

library(ggplot2)
library(reshape2)

# Add time variable as per @BenBolker's suggestion
dt$time <- seq(nrow(dt))

# Use melt to reshape data so values and variables are in separate columns
dt.df <- melt(dt, measure.vars = c("DEPTH", "X", "Y", "Z"))

ggplot(dt.df, aes(x = time, y = value)) +
  geom_line(aes(color = variable)) +
  facet_grid(variable ~ ., scales = "free_y") +
  # Suppress the legend since color isn't actually providing any information
  opts(legend.position = "none")

Plotting multiple y-variables against a common x-variable

answered 2012-08-03T13:09:49.307

Your Answer