title: Editing DLNM Plot Key in R
body: <p>I'm using library(DLNM) in R and I'm trying to modify the range of the key. I want to modify my command so that both plots go from 0.9 to 1.2, even though each does not necessarily have data that goes all the way.</p>
  <p>How do I get both of the plots to have the same key for the data?</p>
  <p>When I've used <code>plot(pred1.var,&quot;contour&quot;,zlim = c(0.9,1.2))</code> I noticed that zlim changes only the height of the 3D plot when I don't have the &quot;contour&quot; command. How can I make the scale ranges and number of levels in the key change?</p>
  <p>SUMMARY: I want the keys of the heat map 1 and 2 plots to both go from 0.9 to 1.2 with the same amount of step intervals. This plot requires having library(DLNM) loaded.</p>
  <p>EXAMPLE</p>
  <pre><code> #Load in libraries
  
  library(splines)
  library(dlnm)
  library(lubridate)
  library(readxl)
  library(mgcv)
  library(ggplot2)
  library(dplyr)
  
  mainran &lt;- runif(5840, min=0, max=100)
  mainran2 &lt;- runif(5840, min=0, max=100)
  ran1&lt;-runif(5840, min=0, max=100)
  ran2&lt;-runif(5840, min=0, max=100)
  
  
  
  #Create Trend Term
  
  Trend = seq(1,5840,1)
  
  
  
  #DLNM component
  
  
  
  #Create a date variable needed in model (&quot;datevar&quot; is a &quot;lubridate&quot; function)
  
  datevar=seq(2005,2021 - 1/365, 1/365)
  
  
  
  #Create a data frame with lagged variables using all variables in &quot;final&quot; GAM
  
  lagframe=data.frame(mainran=mainran, Trend=Trend, mainran2=mainran2, ran1=ran1, ran2=ran2, datevar=datevar)
  
  
  
  #Generate basis matrix for predictors and lags
  
  df.time=4
  
  basis.mainran2=crossbasis(lagframe$mainran2,vartype=&quot;ns&quot;,vardf=3,cen=T,maxlag=21,lagtype=&quot;ns&quot;,lagdf=3,cenvalue=20)
  
  basis.ran1=crossbasis(lagframe$ran1,vartype=&quot;ns&quot;,vardf=3,cen=T,maxlag=21,lagtype=&quot;ns&quot;,lagdf=3,cenvalue=20)
  
  basis.ran2=crossbasis(lagframe$ran2,vartype=&quot;ns&quot;,vardf=3,cen=T,maxlag=21,lagtype=&quot;ns&quot;,lagdf=3,cenvalue=20)
  
  
  
  #Define the position of knots for spline functions
  
  varknotsmainran2=equalknots(lagframe$mainran2,fun=&quot;bs&quot;,df=3,degree=2)
  
  varknotsmainran2
  
  lagknotsmainran2=logknots(21,2)
  
  lagknotsmainran2
  
  varknotsran1=equalknots(lagframe$ran1,fun=&quot;bs&quot;,df=3,degree=2)
  
  varknotsran1
  
  lagknotsran1=logknots(21,2)
  
  lagknotsran1
  
  varknotsran2=equalknots(lagframe$ran2,fun=&quot;bs&quot;,df=3,degree=2)
  
  varknotsran2
  
  lagknotsran2=logknots(21,2)
  
  lagknotsran2
  
  
  
  # Create the basis matrix; set number of lag (&quot;lag=10 (days)&quot;); cen = centering value
  
  cb1.mainran2=crossbasis(lagframe$mainran2,lag=21,argvar=list(fun=&quot;bs&quot;,knots=varknotsmainran2),arglag=list(knots=lagknotsmainran2))
  
  summary(cb1.mainran2)
  
  
  
  # Now run best GAM model as GLM (DLNM uses GLM rather than GAM)
  
  # This is the &quot;best&quot; model from GAMs
  
  modelA1=glm(mainran~cb1.mainran2+ns(ran1, 3)+ns(ran2, 3)+ns(Trend, 16*3),family=quasipoisson(),lagframe)
  
  summary(modelA1)
  
  
  
  # Generate predictions from DLNM and plot results
  
  pred1.mainran2=crosspred(cb1.mainran2,modelA1,by=1)
  
  
  # Plot of &quot;heat map&quot;
  
  plot(pred1.mainran2,&quot;contour&quot;,xlab=&quot;mainran2&quot;,plot.title=title(&quot;Random Comparison Model&quot;,xlab=&quot;mainran2&quot;,ylab=&quot;Lag (days)&quot;)
       ,key.title=title(&quot;Key&quot;))
  
  #Code for Plot 2
  
  mainran02 &lt;- runif(5840, min=0, max=100)
  mainran022 &lt;- runif(5840, min=0, max=100)
  ran12&lt;-runif(5840, min=0, max=100)
  ran22&lt;-runif(5840, min=0, max=100)
  
  #Create Trend Term
  
  Trend = seq(1,5840,1)
  
  #DLNM component
  
  #Create a date variable needed in model (&quot;datevar&quot; is a &quot;lubridate&quot; function)
  
  datevar=seq(2005,2021 - 1/365, 1/365)
  
  #Create a data frame with lagged variables using all variables in &quot;final&quot; GAM
  
  lagframe=data.frame(mainran02=mainran02, Trend=Trend, mainran022=mainran022, ran12=ran12, ran22=ran22, datevar=datevar)
  
  
  #Generate basis matrix for predictors and lags
  
  df.time=4
  
  basis.mainran022=crossbasis(lagframe$mainran022,vartype=&quot;ns&quot;,vardf=3,cen=T,maxlag=21,lagtype=&quot;ns&quot;,lagdf=3,cenvalue=20)
  
  basis.ran12=crossbasis(lagframe$ran12,vartype=&quot;ns&quot;,vardf=3,cen=T,maxlag=21,lagtype=&quot;ns&quot;,lagdf=3,cenvalue=20)
  
  basis.ran22=crossbasis(lagframe$ran22,vartype=&quot;ns&quot;,vardf=3,cen=T,maxlag=21,lagtype=&quot;ns&quot;,lagdf=3,cenvalue=20)
  
  
  #Define the position of knots for spline functions
  
  varknotsmainran022=equalknots(lagframe$mainran022,fun=&quot;bs&quot;,df=3,degree=2)
  
  varknotsmainran022
  
  lagknotsmainran022=logknots(21,2)
  
  lagknotsmainran022
  
  varknotsran12=equalknots(lagframe$ran12,fun=&quot;bs&quot;,df=3,degree=2)
  
  varknotsran12
  
  lagknotsran12=logknots(21,2)
  
  lagknotsran12
  
  varknotsran22=equalknots(lagframe$ran22,fun=&quot;bs&quot;,df=3,degree=2)
  
  varknotsran22
  
  lagknotsran22=logknots(21,2)
  
  lagknotsran22
  
  
  # Create the basis matrix; set number of lag (&quot;lag=10 (days)&quot;); cen = centering value
  
  cb1.mainran022=crossbasis(lagframe$mainran022,lag=21,argvar=list(fun=&quot;bs&quot;,knots=varknotsmainran022),arglag=list(knots=lagknotsmainran022))
  
  summary(cb1.mainran022)
  
  
  # Now run best GAM model as GLM (DLNM uses GLM rather than GAM)
  
  # This is the &quot;best&quot; model from GAMs
  
  modelA1=glm(mainran02~cb1.mainran022+ns(ran12, 3)+ns(ran22, 3)+ns(Trend, 16*3),family=quasipoisson(),lagframe)
  
  summary(modelA1)
  
  
  
  # Generate predictions from DLNM and plot results
  
  pred1.mainran022=crosspred(cb1.mainran022,modelA1,by=1)
  
  # Plot of &quot;heat map&quot;
  
  plot(pred1.mainran022,&quot;contour&quot;,xlab=&quot;mainran022&quot;,plot.title=title(&quot;Random Comparison Model 2&quot;,xlab=&quot;mainran022&quot;,ylab=&quot;Lag (days)&quot;)
       ,key.title=title(&quot;Key&quot;))
  </code></pre>