Haroon Lone
Haroon Lone

Reputation: 2949

Multiplot using ggplot2

I am using ggplot2 for a multiplot. Althoug after a lot of tweaking, I still face problems as:

  1. Some free space gets plotted on each side (left/right) of each plot. I have marked this on the right side of each plot.
  2. Plots are not aligned by the left side. This problem is clearly observed in the bottom plot
  3. Y axis label is much far away from the plots. Can I reduce this separation?

Multiplot is:

enter image description here

I used the following R code for the same:

p1 <- ggplot(data = dplots[[1]],aes(timestamp,power/1000))+ geom_line()+
      ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+labs(title="room1")
p2 <- ggplot(data = dplots[[2]],aes(timestamp,power/1000))+ geom_line()+
  ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="room2")
p3 <- ggplot(data = dplots[[6]],aes(timestamp,power/1000))+ geom_line()+
  ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="room3")                      
p4 <- ggplot(data = dplots[[4]],aes(timestamp,power/1000))+ geom_line()+
  ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="room4")
p5 <- ggplot(data = dplots[[5]],aes(timestamp,power/1000))+ geom_line()+
  ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="room5")
p6 <- ggplot(data = dplots[[3]],aes(timestamp,power/1000))+ geom_line()+
        ylab("")+theme(axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="Chiller") +
       scale_x_datetime(labels= date_format("%d-%m-%y",tz ="UTC"),breaks = pretty_breaks(8)) 
grid.arrange(p1,p2,p3,p4,p5,p6,nrow=6,ncol=1,heights=c(0.15,0.15,0.15,0.15,0.15,0.15),left="Power (KW)")  

The dataset (dplots) is stored at the link.

Upvotes: 3

Views: 2029

Answers (2)

zx8754
zx8754

Reputation: 56004

With your existing code, use cowplot package:

library(cowplot)
plot_grid(p1,p2,p3,p4,p5,p6,ncol=1,align = "v")

enter image description here

Upvotes: 5

Jaap
Jaap

Reputation: 83215

Probably the easiest solution is to combine the dataframes in the list in one dataset. With rbindlist from the data.table package you can also include id's for each dataframe:

library(data.table)
# bind the dataframes together into one datatable (which is an enhanced dataframe)
DT <- rbindlist(dplots, idcol = "id")
# give names to the id's
DT$id <- factor(DT$id, labels = c("room 1","room 2","room 3", "room 4","room 5","Chiller"))

library(ggplot2)
ggplot(DT, aes(x = timestamp, y = power)) + 
  geom_line() + 
  scale_x_datetime(expand = c(0,0)) +
  facet_grid(id ~ ., scales="free_y") +
  theme_bw()

this results in the following plot:

enter image description here

Upvotes: 10

Related Questions