Web Content Display

Web Content Display (CoastWatch User Forum)

The NOAA CoastWatch Forum provides information in several categories.  The information is relevant to CoastWatch products and software (https://coastwatch.noaa.gov)  and vary in technical details.  For questions and issues not addressed here,  start a thread in the appropriate category or contact the CoastWatch HelpDesk (coastwatch.info@noaa.gov).  Please note our Privacy Policy (https://www.noaa.gov/privacy.html) and that this is a public forum.  All information is voluntarily and anonymous submissions are accepted pending review prior to posting. 

Forums

Back

RE: exporting pdf maps R

ES
Eva Schemmel, modified 4 Years ago.

exporting pdf maps R

Youngling Posts: 13 Join Date: 4/13/20 Recent Posts

Making nice pdf maps is giving me trouble. I want to export as a pdf using. pdf("mariana_chlor_log2.pdf", width=4, height = 3, layout(matrix(c(1,2,3,4), nrow=1, ncol=2), widths=c(3,1), heights=4) ). 

I end up getting an empty 2 panel layout. Any ideas what I can do to correct this?

 

 

pdf("mariana_chlor_log2.pdf", width=4, height = 3, layout(matrix(c(1,2,3,4), nrow=1, ncol=2), widths=c(3,1), heights=4) )

#layout(matrix(c(1,2,3,4), nrow=1, ncol=2), widths=c(3,1), heights=4) 
layout.show(2)

par(mar=c(3,3,3,1)) 
land <- maps::map("world2", fill = TRUE, col='grey',xlim = xlim, ylim = ylim, plot=FALSE, xaxs='i',yaxs='i',asp=1, cex.axis = 3, proj4string = CRS("+proj=longlat +datum=WGS84"))

ids <- sapply(strsplit(land$names, ":"), function(x) x[1]) 
bPols <- map2SpatialPolygons(land, IDs = ids, proj4string = CRS("+proj=longlat +datum=WGS84")) 

plot(bPols, col = "grey", axes = FALSE, xlim = xlim, ylim = ylim, xaxs='i',yaxs='i',asp=1, cex.axis = 3, main="Chlor a") 
x = seq(140, 150, 1) 
axis(1, x, sapply(paste(x, "E", sep = ""), function(x) x[1])) 

y = seq(12, 22, 2) 
axis(2, las = 1, y, sapply(paste(y, "N", sep = ""), function(x) x[1])) 

box()

temp.range=list()
for (j in 1:10) {
  print(j)
  I=which(mariana[,4]==j)  #select the feature #
  poly=mariana[I,1:2]
  #write.csv(mariana,'mariana.csv') 
  
  #We are only interested in column 2 and 3 of pnmn.csv, which correspond to the longitudes and latitudes of the boundary, respectively.
  #poly=read.csv('mariana.csv',header=TRUE)[,2:3]
  
  poly=data.frame(poly)
  names(poly) <- c("lon","lat")
  #We need to convert the longitudes to 0-360? to make it easier to work across the dateline.
  I=which(poly$lon<0)
  if (length(I)>0) poly$lon[I]=poly$lon[I]+360
  
  ERDDAP_Node="https://oceanwatch.pifsc.noaa.gov/erddap/"
  dataInfo <- rerddap::info('esa-cci-chla-2009-2018-clim-v4-2', url=ERDDAP_Node)
  #Let's look at the variables in our dataset:
  dataInfo$variable$variable_name
  #[1] "analysed_sst" "sea_ice_fraction"
  #We are interested in sea surface temperature data, so we need to select the fist variable:
  #analysed_sst
  
  parameter=dataInfo$variable$variable_name[1]
  
  xcoord <- poly$lon 
  ycoord <- poly$lat
  
  chl <- rxtractogon (dataInfo, parameter=parameter, xcoord=xcoord, ycoord=ycoord, tcoord=tcoord)
  chl$time ##only two time steps here! why not all 12
  
  #We need to transform our extracted chl data for that time step into a matrix for the image function to work properly. This chl dataset is also in Kelvin degrees, so let-s convert it to Celsius degree:
  test=as.matrix(chl$chlor_a[,,i])
  #want to get annual mean of chl for 2019
  #mean_chl=as.matrix(apply(chl$chlor_a,c(1,2),mean,na.rm=TRUE))
  #chl.yr=apply(chl[,,1:12],c(1,2),mean,na.rm=TRUE)
  test=as.matrix(log(chl$chlor_a[,,i]))
  
  
  #Let's make pretty axes:
  #We'll add the color plot on top of the map:
  par(new=TRUE)
  image(chl$longitude,chl$latitude,test,col=c,breaks=breaks,xlab='',ylab='', axes=FALSE, xlim = xlim, ylim = ylim,xaxs='i',yaxs='i',asp=1, las=1)
  
  #plot the boundary lines
  lines(poly$lon,poly$lat,lwd=1)
  temprange<-c(min=min(chl$chlor_a, na.rm=TRUE),max=max(chl$chlor_a, na.rm=TRUE),mean=mean(chl$chlor_a, na.rm=TRUE), L=j) #tryed total=sum(chl$chlor_a, na.rm=TRUE) here did not work
  
  temp.range[[j]]<-temprange
}

#Plot color scale using the scale.R file:
source('scale.R')
#par(mar=c(3,1,3,3))
image.scale(test, col=c, breaks=breaks, horiz=FALSE, yaxt="n",xlab='',ylab='',main='log chlor a') 
axis(4,las=1) 
box()

dev.off()

MA
Melanie Abecassis, modified 4 Years ago.

RE: exporting pdf maps R

Youngling Posts: 65 Join Date: 6/12/17 Recent Posts

Hi Eva,

Instead of
​​​​​​​#layout(matrix(c(1,2,3,4), nrow=1, ncol=2), widths=c(3,1), heights=4) 
layout.show(2)

 

Try the opposite:

layout(matrix(c(1,2,3,4), nrow=1, ncol=2), widths=c(3,1), heights=4) 
#layout.show(2)

But if I recall correctly, pdf and postscript have issues with the layout function. Not sure... I usually save as jpeg or png.

 

ES
Eva Schemmel, modified 4 Years ago.

RE: exporting pdf maps R

Youngling Posts: 13 Join Date: 4/13/20 Recent Posts
Hi Melanie,
Thanks for the advice. It looks like it is just a bug with pdf(). 
Best,
Eva

On Fri, Nov 6, 2020 at 5:53 AM Melanie Abecassis <VLab.Notifications@noaa.gov> wrote:

Hi Eva,

Instead of
​​​​​​​#layout(matrix(c(1,2,3,4), nrow=1, ncol=2), widths=c(3,1), heights=4) 
layout.show(2)

 

Try the opposite:

layout(matrix(c(1,2,3,4), nrow=1, ncol=2), widths=c(3,1), heights=4) 
#layout.show(2)

But if I recall correctly, pdf and postscript have issues with the layout function. Not sure... I usually save as jpeg or png.

 


--
Melanie Abecassis CoastWatch Virtual Lab Forum https://vlab.noaa.gov/web/coastwatch/coastwatch-knowledge-base/forum/-/message_boards/view_message/12451857 VLab.Notifications@noaa.gov



--
Eva Schemmel, PhD
Fisheries Biologist
Pacific Islands Fisheries Science Center
NOAA Fisheries
808-725-5513