I'm having a hard time plotting an sf
point object.
Basically I want to show the points with a black border and a fill color according to a variable.
In base plot I would do this to impose border color with different background colors:
plot(1:3, cex = 3, pch = 21, col = 'red', bg = c('blue', 'green', 'yellow'), lwd = 2)
I use pch = 21
in order to fill the points with bg
colors while using col = 'red'
as the point border color.
Using a sf
object I can use col
to impose the border color but the legend disappears (according to the documentation for plot {sf}
, if col
is passed to the function the legend is suppressed).
library(sf)
# example
df <- data.frame(est = c('A', 'B', 'C'),
long = c(-70, -68, -69),
lat = c(-12.2, -12.4, -12),
RMSE = c(25, 8, 55)) %>%
st_as_sf(coords = c('long', 'lat'), crs = 4326)
# reclassifying the RMSE variable:
df$rmse_cut <- cut(df$RMSE, c(0,15,30,45,60))
# plotting the map
plot(df['rmse_cut'], main = 'RMSE', graticule = TRUE, axes = TRUE,
bgc = 'gray92',
pch = 21, lwd = 2, cex = 3,
col = 'black',
bg = c('blue', 'green', 'yellow'))
But plotting without col
:
plot(df['rmse_cut'], main = 'RMSE', graticule = TRUE, axes = TRUE,
bgc = 'gray92',
pch = 21, lwd = 2, cex = 3,
bg = c('blue', 'green', 'yellow'))
The borders are sorted by the variable rmse_cut
but the background color is fixed.
How can I use a single color for point borders while using background colors to show the different values from the rmse_cut
variable?.
ggplot2
option but I realize I have many scripts inbase
R. Perhaps finding a solution to this would save me some time (I think it'll take quite a lot of time to recreate my old plots withggplot2
). Anyway, thank you. – noriega