Krithi.S
Krithi.S

Reputation: 55

Vertical stack plots at same time series x axis in R

I have gone through the following link Stacking multiple plots and I am using the below r commands. I am getting the graph of 4 stack y axis and Time series x axis.

library(tidyverse)
library(reshape2)
dt$time <- seq(nrow(dt))
dt.df <- melt(dt, measure.vars = c("Time", "A", "B", "B_1", "C"))
ggplot(dt.df, aes(x = Time, y = value)) +
facet_grid(variable ~ ., scales = "free_y") +
theme(legend.position = "none") 

Below is the Sample data frame

Time        A       B       B_1     C
10:12:54    2376.2  1.462   3.462   48
10:12:55    2410    1.462   3.462   48
10:12:56    2400    1.462   3.462   48
10:12:57    2409    1.462   3.462   48.6
10:12:58    2400    1.462   3.462   48.6
10:12:59    2385.1  1.462   3.462   46.6
10:13:00    2400    1.462   3.462   46.6
10:13:01    2410    1.462   3.462   46.6
10:13:02    2400    1.462   3.462   46.6
10:13:03    2106    1.463   3.463   46.6
10:13:04    2406    1.463   3.463   44.8
10:13:05    2376.2  1.463   3.463   44.8
10:13:06    2406    1.463   3.463   44.8
10:13:07    2400    1.463   3.463   44.8

I would like to plot vertically stacked A value,B and B_1 value merged together and C value. But the problem is that i can not able to merge B and B1 curve in the same stack. Is it possible to make one only stack with 2 column values and rest others as a single column values? How can I solve this?

Upvotes: 0

Views: 879

Answers (2)

TichPi
TichPi

Reputation: 146

Generate demo data:

Generate example data:

dt = read_table("Time        A       B       C       D
10:12:54    2376.2  1.462   3.462   48
10:12:55    2410    1.462   3.462   48
10:12:56    2400    1.462   3.462   48
10:12:57    2409    1.462   3.462   48.6
10:12:58    2400    1.462   3.462   48.6
10:12:59    2385.1  1.462   3.462   46.6
10:13:00    2400    1.462   3.462   46.6
10:13:01    2410    1.462   3.462   46.6
10:13:02    2400    1.462   3.462   46.6
10:13:03    2106    1.463   3.463   46.6
10:13:04    2406    1.463   3.463   44.8
10:13:05    2376.2  1.463   3.463   44.8
10:13:06    2406    1.463   3.463   44.8
10:13:07    2400    1.463   3.463   44.8")
dt$Time=as.POSIXct(dt$Time)

If you want to plot it quickly, try this:

library(foqat)
geom_ts_batch(dt2, panelgap=3)

View Figure 1

If you want to plot it in detail, try this:

library(foqat)
library(patchwork)
blankx=theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank())
p2=geom_ts(dt, yl=2, llist=2, lcc="blue", yllab="A")+blankx
p3=geom_ts(dt, yl=3, llist=3, lcc="red", yllab="B")+blankx
p4=geom_ts(dt, yl=4, llist=4, lcc="green", yllab="C")+blankx
p5=geom_ts(dt, yl=5, llist=5, lcc="grey", yllab="D", xlab="Time")
p2/p3/p4/p5

View Figure 2

Upvotes: 0

Mojoesque
Mojoesque

Reputation: 1318

I think the key is to add another column to your data specifying the facets. The column is basically the same as the variable column except "B" and "B_1" have the same value, putting them in the same facet.

Here is one example using gather instead of melt.

library(tidyverse)
dt = read_table("Time        A       B       B_1     C
10:12:54    2376.2  1.462   3.462   48
10:12:55    2410    1.462   3.462   48
10:12:56    2400    1.462   3.462   48
10:12:57    2409    1.462   3.462   48.6
10:12:58    2400    1.462   3.462   48.6
10:12:59    2385.1  1.462   3.462   46.6
10:13:00    2400    1.462   3.462   46.6
10:13:01    2410    1.462   3.462   46.6
10:13:02    2400    1.462   3.462   46.6
10:13:03    2106    1.463   3.463   46.6
10:13:04    2406    1.463   3.463   44.8
10:13:05    2376.2  1.463   3.463   44.8
10:13:06    2406    1.463   3.463   44.8
10:13:07    2400    1.463   3.463   44.8")

dt.df = gather(dt, "variable", "value", -Time) %>%
  mutate(facets = variable,
         facets = if_else(variable == "B_1", "B", facets))

dt.df %>%
  ggplot(aes(x = Time, y = value, color = variable)) +
  geom_line() +
  facet_grid(facets ~ ., scales = "free_y")

enter image description here

With this example data you don't see the change in either "B" or "B_1" because when they are on the same scale the respective change is too small. But it illustrates one way to approach the problem. I added the legend again to be able to differentiate between "B" and "B_1".

Upvotes: 1

Related Questions