I'm trying to replicate using ggplot2, an earlier question i had using leaflet. I'm trying to merge two palettes where one is used if my variable is below a certain threshold and another if above a certain threshold. As I did with my leaflet question my final plot doesn't seem to be correct. I also want to convert the factor legend to a continuous one. Example code:
library(sf)  
library(leaflet)
library(RColorBrewer)
# preparing the shapefile
nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>%
  st_transform(st_crs(4326)) %>%
  st_cast('POLYGON')
# Create 108 categories
mutate(nc, category = ntile(PERIMETER, n = 108)) -> nc
x <- sum(nc$PERIMETER < 1.3)  
x # number of values below threshold = 21
### Create an asymmetric color range
## Make vector of colors for values smaller than 1.3 (21 colors)
rc1 <- colorRampPalette(colors = c("#006837", "#1A9850"), space = "Lab")(x)    #21
## Make vector of colors for values larger than 1.3
rc2 <- colorRampPalette(colors = c("#FDAE61", "#A50026"), space = "Lab")(length(nc$PERIMETER) - x)
## Combine the two color palettes
rampcols <- c(rc1, rc2)
mypal <- colorFactor(palette = rampcols, domain = nc$category)
previewColors(colorNumeric(palette = rampcols, domain = NULL), values = 1:length(nc$PERIMETER))
nc %>%
  ggplot() +
  geom_sf(aes(fill = factor(category))) +
  scale_fill_manual(values = mypal(nc$category), guide = FALSE) 
gives this plot:
but the leaflet plot (correct coloring)
leaflet() %>%
  addTiles() %>%
  addPolygons(data = nc,
              fillOpacity = 0.7,
              fillColor = ~mypal(nc$category),
              popup = paste("PERIMETER: ", nc$PERIMETER)) %>%
  addLegend( pal = mypal, values = nc$category)
gives this correct plot:
You can see the ggplot figure is not the same which I can't fix.
Additionally when I add a legend to the ggplot, I get a factor legend for the new palette but I want a continuous one (even if the variable is a factor):
nc %>%
  ggplot() +
  geom_sf(aes(fill = factor(category))) +
  scale_fill_manual(values = mypal(nc$category))
Would anyone know how to fix the coloring issue and alter the associated legend in continuous form? Thanks



 
    
 
    