2

Can anyone tell me how to change the labels in a legend created by spplot in R? It is a typical vertical ribbon legend automatically generated by spplot. Based on color, it is labelled 1-7 but I would like to label it 1%-7% and make the labels larger.

I understand that the graphics are based on lattice but it is still difficult to find anywhere how to do what, I would think, would be fairly straight forward.

The code I have is

plot2 <- spplot(mymap, "percentage", col.regions = plotclr, 
                     at = round(class$brks, digits=1) )

where: mymap is a typical shape file with an attributes column called percentage which pertains to certain map regions; plotclr is my vector of colors, class$brks are the divisions of the percentages to which the colors correspond. The code works fine. I just can't change the legend labeling.

3

I found an answer. I basically take everything I need into the colorkey function as follows.

plot3 <- spplot(mymap, "percentage", col.regions = plotclr,
                    at = round(class$brks, digits=1),
colorkey = list(labels = list( labels = c("0%", "1%","2%","3%","4%","5%","6%","7%"),        
                width = 2, cex = 2)))

Width changes the width of the ribbon legend. It is also possible to change it's height with height = some fraction of 1 = full height.

Looks like it was straight forward after all.

I found it, after much searching, under help(levelplot) in R.

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.