How to plot different projections with subplot

I want to plot a column of data in Mollweide projection, and another column in orthographic projection. My idea was to use subplot to define a fixed size of each subpanel, and then use two different subplot commands to change projections. My code is below. The problem is that the rows don’t align correctly. Please advise if you can spot the problem.

#!/usr/bin/env bash

CBMAX="40"
CBSTEP="20"
INPUT_FILE="bin_median.txt"
EQMERIDIAN="180" # center equatorial view on 180 deg (12 MLT)

# subplot dimensions
SUBPLOT_WIDTH="3i"
SUBPLOT_HEIGHT="3i"

XNODES=$(cat $INPUT_FILE | grep "x nodes" | sed -e 's/.*x nodes: \([0-9]\+\)/\1/')
YNODES=$(cat $INPUT_FILE | grep "y nodes" | sed -e 's/.*y nodes: \([0-9]\+\)/\1/')

XYZFILE=$(mktemp)
GRDFILE=$(mktemp)

gmt set MAP_FRAME_TYPE plain
gmt makecpt -D -Cjet -T-${CBMAX}/${CBMAX} > t.cpt

gmt set MAP_TITLE_OFFSET -10p

gmt begin Fig_observations png
  gmt set FONT_TAG 16p,Times-Italic
  gmt set FONT_TITLE 16p,Helvetica

  gmt subplot begin 2x2 -Fs${SUBPLOT_WIDTH}/${SUBPLOT_HEIGHT} -JW${EQMERIDIAN}/18c -Rg -Bxg90 -Byg60 -A+o5p -M-0.5c/-0.5c

    # Br component
    cat ${INPUT_FILE} | grep -v "^#" | awk '{print $1,$2,$3}' > ${XYZFILE}
    gmt xyz2grd ${XYZFILE} -G${GRDFILE} -Rd -I${XNODES}+/${YNODES}+
    gmt subplot set 0,0 -A"B@-r@-"
    gmt grdimage ${GRDFILE} -Ct.cpt -B+t"Data"

    # Bt component
    cat ${INPUT_FILE} | grep -v "^#" | awk '{print $1,$2,$4}' > ${XYZFILE}
    gmt xyz2grd ${XYZFILE} -G${GRDFILE} -Rd -I${XNODES}+/${YNODES}+
    gmt subplot set 1,0 -A"B@-@~\161@~@-"
    gmt grdimage ${GRDFILE} -Ct.cpt

  gmt subplot end

  gmt subplot begin 2x2 -Fs${SUBPLOT_WIDTH}/${SUBPLOT_HEIGHT} -JG0/90/4c -Rg -Bxg90 -Byg60 -A+o5p -M-0.5c/-0.5c

    # Br component
    cat ${INPUT_FILE} | grep -v "^#" | awk '{print $1,$2,$3}' > ${XYZFILE}
    gmt xyz2grd ${XYZFILE} -G${GRDFILE} -Rd -I${XNODES}+/${YNODES}+
    gmt subplot set 0,1 -A"B@-r@-" -JG0/90/4c
    gmt grdimage ${GRDFILE} -Ct.cpt -B+t"Data"

    # Bt component
    cat ${INPUT_FILE} | grep -v "^#" | awk '{print $1,$2,$4}' > ${XYZFILE}
    gmt xyz2grd ${XYZFILE} -G${GRDFILE} -Rd -I${XNODES}+/${YNODES}+
    gmt subplot set 1,1 -A"B@-@~\161@~@-"
    gmt grdimage ${GRDFILE} -Ct.cpt

  gmt subplot end

  gmt colorbar -Ct.cpt -DJBC+w6i/0.5c -Bx${CBSTEP} -By+l"nT" -Y0.7c
  rm -f $XYZFILE $GRDFILE
gmt end show

The resulting figure is here:

The data file is here: https://ufile.io/pjoyxid4

You don’t have to specify the projection within the subplot command line, thus you can do

subplot begin […]
    subplot set 0
        plot {projA}
    subplot set 1
        plot {projB}
…


When I remove the -J option from the subplot begin command, I get this error:

plot [ERROR]: Could not parse -nan into Geographical, Cartesian, or Temporal coordinates!plot [ERROR]: Option -R parsing failure. Correct syntax:
-R///[//]
Append +r if giving lower left and upper right coordinates
-Rg or -Rd for global domain
-R to take the domain from a grid file
plot [ERROR]: Offending option -R0/3/0/-nan

Just making sure we are talking GMT 6.4.0 or master 6.5.x? If not, upgrade.

Hello @pa345,

welcome to the GMT forum :slightly_smiling_face:!

I feel the issue here is that both the panel size (via -Fs as 3i/3i) and the sizes of the maps (18c for the Mollwide projections of the first 2x2 supblot and 4c for the Orthographic projections of the second 2x2 subplot) are given. As already mentioned by @PlanetGus it is not needed to give -R and -J within the subplot begin line, but you can set both for each panel separately. As far as I know, in subplot mode, you have to use a ? for the size of the map or projection. Maybe you can use the basic code example below as orientation and expand it for your needs (maybe -Fs3i/1.5i looks nicer).

Code example:

gmt begin
    
    gmt subplot begin 2x2 -Fs3i/3i
    
        gmt subplot set 0,0
        gmt basemap -Rg -JW180/? -B
        
        gmt subplot set 1,0
        gmt basemap -Rg -JW180/? -B
        
        gmt subplot set 0,1
        gmt basemap -Rd -JG0/90/? -B
        
        gmt subplot set 1,1
        gmt basemap -Rd -JG0/90/? -B
    
    gmt subplot end
    
gmt end show

Output figure:

Thank you all for taking the time to help me with this. Upgrading to 6.4.0 was important (I was using 6.2.0). I took yvonne’s example and tried to go a bit further to what I want to eventually have, which is a 2x3 grid with Mollweide, ortho, ortho. I tried the suggestion of -Fs3i/1.5i which seems to have nicer proportions, however now there is quite a lot of horizontal whitespace:

gmt begin

    gmt subplot begin 2x3 -Fs3i/1.5i -M0.5c/0.5c

        gmt subplot set 0,0
        gmt basemap -Rg -JW180/? -B

        gmt subplot set 1,0
        gmt basemap -Rg -JW180/? -B

        gmt subplot set 0,1
        gmt basemap -Rd -JG0/90/? -B

        gmt subplot set 1,1
        gmt basemap -Rd -JG0/90/? -B

        gmt subplot set 0,2
        gmt basemap -Rd -JG0/90/? -B

        gmt subplot set 1,2
        gmt basemap -Rd -JG0/90/? -B

    gmt subplot end

gmt end show

The result is:

Does anyone have a suggestion to reduce the whitespace horizontally between each panel, and having the same amount of white space between each figure?

I thought it might look better if I made it ortho, Mollweide, ortho for better symmetry. In this case, the code is,

gmt begin tmp png

    gmt subplot begin 2x3 -Fs3i/1.5i -M0.5c/0.5c

        gmt subplot set 0,0
        gmt basemap -Rd -JG0/90/? -B

        gmt subplot set 0,1
        gmt basemap -Rg -JW180/? -B

        gmt subplot set 0,2
        gmt basemap -Rd -JG0/90/? -B

        gmt subplot set 1,0
        gmt basemap -Rd -JG0/90/? -B

        gmt subplot set 1,1
        gmt basemap -Rg -JW180/? -B

        gmt subplot set 1,2
        gmt basemap -Rd -JG0/90/? -B

    gmt subplot end

gmt end show

Now the figure looks like

It looks a bit nicer (more symmetric), but still too much whitespace. Any ideas?

Read the full -F documentation about variable panel width