Customize labels on a color key using base R

174 views Asked by At

Consider this plot, which shows values ranging from 0.042 to 0.241:

library(sf)
demo(nc, ask=FALSE, echo=FALSE)
brks <- seq(0, 0.2, by=0.05)
plot(nc["AREA"], key.pos=1, breaks=brks, at=brks)

enter image description here

However, let's say I need to customize the color key labels. I want add one "segment" to it in order to include values higher than 0.2.

For example:

brks2 <- c(seq(0, 0.2, by=0.05), 1)
plot(nc["AREA"], key.pos=1, breaks = brks2, at=brks2)

enter image description here

but the color key, rather than getting "stretched" from 0.2 to 1, should have just one more segment, with the label >0.2 or similar.

How can I do that?

1

There are 1 answers

0
jay.sf On

We could add .25 to the breaks, but I didn't find an option in sf:::plot.sf to create custom labels, so last label would be 0.25. Maybe you could set at=2 (to print the labels outside the window) and build a custom axis and labels using mtext, but this becomes very cumbersome and inaccurate.

It might, thus, be better to use nbreaks=. You could also play around with breaks=, where "pretty" is default and other options are "fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust", "bclust", "fisher", "jenks", "dpih", "headtails", see ?sf:::plot.sf.

sf:::plot.sf(nc["AREA"], key.pos=1, breaks="pretty", nbreaks=5)

enter image description here

I think it's reasonable to address the author with a feature request, or maybe someone else is able to hack sf:::plot.sf.