0

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

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

par(xpd=TRUE)
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 '18 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 '18 at 10:53
1

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.

library(raster)

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

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

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

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

0

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

myraster<-as.factor(myraster)

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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