riatelab / cartography

Thematic Cartography for R
http://riatelab.github.io/cartography/docs/
397 stars 46 forks source link

color gradient for gradLinkLayer (feature proposal) #46

Closed damienjacques closed 5 years ago

damienjacques commented 5 years ago

Using a simple workaround, I have added color gradient support to gradLinkLayer function. You may want to consider adding this feature to the package.

Here are the changes I have made:

In gradLinkLayer:

plot(sf::st_geometry(link), 
       col = if(length(col) > 1){col[as.factor(lwdMap)]}else{col},
       lwd = lwdMap, add = add)

In legendGradLines:

segments(xref, yref + height/2 + i * height + i * delta2 + 
                (height + delta2)/2, xref + width, yref + i * height + 
                 i * delta2 + height/2 + (height + delta2)/2, lwd = lwd[j], 
                 col = ifelse(length(col > 1), col[j], col),
                 lend = 1)

Example of output

library(sp)
data("nuts2006")
# Create a link layer
twincities.sf <- getLinkLayer(x = nuts2.spdf, df = twincities.df[,1:2])

Initial function

figdim <- getFigDim(x = districtsShp, width = 800, mar = c(0,0,1.2,0), res = 100)
cols <- c('#edf8fb','#b3cde3','#8c96c6','#8856a7','#810f7c')

# set figure margins
par(mar = c(0,0,1.2,0))
# Plot the links - Twin cities agreements between regions 
plot(nuts0.spdf, col = "grey60",border = "grey20")
gradLinkLayer(x = twincities.sf, df = twincities.df,
              legend.pos = "topright",
              var = "fij", breaks = c(2,5,15,20,30), lwd = c(0.1,1,4,10),
              col = "#92000090", add = TRUE)

rplot06

Modified function

figdim <- getFigDim(x = districtsShp, width = 800, mar = c(0,0,1.2,0), res = 100)
cols <- c('#edf8fb','#b3cde3','#8c96c6','#8856a7','#810f7c')
# set figure margins
par(mar = c(0,0,1.2,0))
# Plot the links - Twin cities agreements between regions 
plot(nuts0.spdf, col = "grey60",border = "grey20")
gradLinkLayer_modif(x = twincities.sf, df = twincities.df,
              legend.pos = "topright",
              var = "fij", breaks = c(2,5,15,20,30), lwd = c(0.1,1,4,10),
              col = c('#ffffb2','#fecc5c','#fd8d3c','#e31a1c'), add = TRUE)

rplot05

rCarto commented 5 years ago

HI, I'll think about this feature, your implementation seems nice. For now this map can be done with gradLinkTypoLayer() with a variable containing a qualitative variable describing the links...

damienjacques commented 5 years ago

Ok thank you