Vint
Vint

Reputation: 499

Two different plots from different libraries on the same page

I have one plot (levelplot) from the lattice library, and a stick plot from the oce library, and I'd like to stack them on top of each other. I'm having problems stacking these plots as I would normally using base R, and I assume it is because these are both specialized plots. Is it possible to stack the two below plots on the same page so that the timestamps (x axis) line up with each other?

library(oce)
library(lattice)
library(grid)

First Plot (example from help docs of OCE)

# Oceanographic example
data(met)
t <- met[["time"]]
u <- met[["u"]]
v <- met[["v"]]
p <- met[["pressure"]]

dev.new(width=15, height=4)
plotSticks(t, 99, u, v, yscale=25)

Second plot:

AllT<-NULL
for(i in seq(1,length(t))){

    myTime<-t[i]
    myTime2<-rep(myTime,10)
    AllT<-append(AllT,myTime2)      
}

myY<-NULL
for(j in seq(1,length(t))){
    mySeq<-seq(1,10)
    myY<-append(myY,mySeq)    
}

Temp<-seq(1,7200)


MyDF<-data.frame(Temp,myY,AllT)



#Plot code
dev.new(width=15, height=6)
p1<-levelplot(Temp ~ AllT * myY,
    data = MyDF,ylim=c(10,1),
    xlab = "Time", ylab = "y]",
    aspect=0.4,
    )

p1

trellis.focus("legend", side="right", clipp.off=TRUE, highlight=FALSE)
grid.text('[Temp]', 1.9, .5, hjust=0.5, vjust=1,rot=270)
trellis.unfocus()

Ideally, the final plot would have the stick plot on top (no need for tick labels on the stick plot if x axis are lined up), with the level plot below with minimal space in between

Upvotes: 1

Views: 139

Answers (1)

Anonymous coward
Anonymous coward

Reputation: 2091

This is as good as I can figure out for now. There's got to be a way to actually modify the grobs like I'm aware of, but it doesn't like the gTrees when I try. So this gets you close.

These links may be of some use.

https://www.andrewheiss.com/blog/2016/12/08/save-base-graphics-as-pseudo-objects-in-r/

force a regular plot object into a Grob for use in grid.arrange

#libraries
library(gridGraphics)
library(grid)
library(gridExtra)
library(oce)
library(gggplot2)
library(ggplotify)

#plots
data(met)
t <- met[["time"]]
u <- met[["u"]]
v <- met[["v"]]
p <- met[["pressure"]]

grab_grob <- function(){
  grid.echo()
  grid.grab()
  }

plotSticks(t, 99, u, v, yscale=25)
p1 <- grab_grob() #grab the last plot as a grob

AllT <- NULL
for(i in seq(1, length(t))){
  myTime <- t[i]
  myTime2 <- rep(myTime, 10)
  AllT <- append(AllT, myTime2)      
}

myY <- NULL
for(j in seq(1, length(t))){
  mySeq <- seq(1, 10)
  myY <- append(myY, mySeq)    
}

Temp <- seq(1, 7200)

MyDF <- data.frame(Temp, myY, AllT)

#Plot code
dev.new(width = 15, height = 6)
p2 <- levelplot(Temp ~ AllT * myY,
          data = MyDF, ylim=c(10, 1),
          xlab = "Time", ylab = "y]",
          aspect = 0.4,
)

p2

trellis.focus("legend", side="right", clipp.off = TRUE, highlight = FALSE)
grid.text('[Temp]', 1.9, .5, hjust = 0.5, vjust = 1, rot = 270)
trellis.unfocus()

p2a <- as.grob(p2)

grid.arrange(p1, p2a, ncol = 1)

Upvotes: 1

Related Questions