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

前端 未结 4 586
既然无缘
既然无缘 2020-12-24 14:39

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 wit

4条回答
  •  情深已故
    2020-12-24 15:21

    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

提交回复
热议问题