Search code examples
rraster

plotRGB add title


I would like to add a title to a plotRGB output.

#create random raster stack
r1 <- raster(nrow=5, ncol=5)
values(r1) <- 1:25

r2 <- r3 <- r1  
RGB_stack <- stack(r1, r2, r3)

I have tried:

# Create an RGB image from the raster stack
plotRGB(RGB_stack, r = 1, g = 2, b = 3,
        axes=TRUE, main="My Title")


plotRGB(RGB_stack, r = 1, g = 2, b = 3,
        axes=TRUE, addfun="(title='Test title')")

I have tried to turn the axes on and off. I am not sure what else to try to make this work.


UPDATE: based upon feedback below i figured out that

#Create an RGB image from the raster stack
plotRGB(RGB_stack, r = 1, g = 2, b = 3,
        axes=TRUE, main="My Title")

works IF you have cleared out previous plots that might have the axes turned off. However i'd still like the plot to be an image with no axes ticks and just a title.


Solution

  • Your first guess works for me:

    plotRGB(RGB_stack, r = 1, g = 2, b = 3,
            axes=TRUE, main="My Title")
    

    enter image description here

    After discussion, it is true that putting axes=FALSE also removes the title. As a workaround, you could try to plot the axis and labels in white. I also add a white box.

    original_par <-par() #original par
    par(col.axis="white",col.lab="white",tck=0)
    plotRGB(RGB_stack, r = 1, g = 2, b = 3,
            axes=TRUE, main="My Title")
    box(col="white")
    par(original_par) # go back to original par
    

    enter image description here