I have the following RasterLayer containing categorical values for each pixel. I am using the following code to plot it

plot(LC, legend=FALSE, col=c("lightcyan3","royalblue","forestgreen","coral3","papayawhip"),xaxt='n', yaxt='n') 

legend("bottom", legend=c("Urban","Water","Forest","Agriculture", "Bare soil"),fill=c("lightcyan3","royalblue","forestgreen","coral3","papayawhip"),horiz = TRUE, inset=-0.175)

enter image description here

In total I have 5 classes but due to the configuration of the legend only 4 are swhon. I want all the elements of the legend to be closer. I have tried to used text.width=c(1,1,1,1) for example to set the distances but it is not doing the job.

I am using a similar approach as the one proposed here and was considering also to use the rasterVis package

  • 1
    Your code works perfectly for me - I get all five categories. Is this in RStudio or is it R's standard graphics output window? Have you tried this in a fresh R session as the first graphic you make (because sometimes previous plots can mess up graphics parameters).
    – Spacedman
    Jan 31, 2018 at 9:45
  • I am using RStudio. I was considering previous plots were the cause. Due to my chain, this one is always produced after other plots. What I have done to try to solve it is: at the beginning of the script graphics.off and opar<-par(). Then, just before ploting this one I call par(opar) and then the code shown on the question.
    – GCGM
    Jan 31, 2018 at 10:53

2 Answers 2


AS @spacedman point it out, I think the problem is caused by previous plots that I have to create. Since I cannot have a fresh R session at this point, I am using a solution based on this post and @ChristopherStephan link. I solved my issue using the rasterVis package, which I find more convinient than using regular plot for raster.

Instead of using the code shown on the question, this one does the job.

First, I use as.factor to arrange my original raster layer


Then I add an extra attribute that will be used for the legend

tar[["landcover"]]<-c("Class1", "Class2")

Then I plot using levelplot from the rasterVis package

levelplot(myraster, col.regions=c("red","white"))

The legend shows now my classes and its size is appropriate

enter image description here


Have you tried to pass cex = 0.75 in the legend function call?

However, I have no problems with the spacing for the legend with or without cex.


ras <- raster(ncol = 100, nrow = 100)
ras <- setValues(ras, (1:ncell(ras)) / 100 + rnorm(ncell(ras)) - 50)

par(xpd = FALSE)
  legend = FALSE,
  col = c(
  xaxt = 'n',
  yaxt = 'n'

par(xpd = TRUE)
  legend = c("Urban", "Water", "Forest", "Agriculture", "Bare soil"),
  fill = c(
  horiz = TRUE,
  inset = -0.175

If you want to use the rasterVis package there is an example for categorical rasters here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.