Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alpha option for hcl.colors pals #14

Merged
merged 1 commit into from
Apr 23, 2021
Merged

Conversation

PaulC91
Copy link
Contributor

@PaulC91 PaulC91 commented Apr 15, 2021

Salut Timothée!

Based on our discussion about using maptile layers with mapsf, I thought it would be useful to have the alpha argument of hcl.colors accessible via all mf_ functions that have a pal argument, so that palettes can be easily mapped with transparency, retaining some visibility of the tile layer below.

Let me know your thoughts.

Paul

@codecov
Copy link

codecov bot commented Apr 15, 2021

Codecov Report

Merging #14 (10643dd) into master (0574658) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 10643dd differs from pull request most recent head b9ee9bb. Consider uploading reports for the commit b9ee9bb to get more accurate results
Impacted file tree graph

@@           Coverage Diff           @@
##           master      #14   +/-   ##
=======================================
  Coverage   95.42%   95.43%           
=======================================
  Files          46       46           
  Lines        2472     2475    +3     
=======================================
+ Hits         2359     2362    +3     
  Misses        113      113           
Impacted Files Coverage Δ
R/mf_map.R 100.00% <ø> (ø)
R/mf_doc_utils.R 100.00% <100.00%> (ø)
R/mf_map_c.R 97.43% <100.00%> (ø)
R/mf_map_pc.R 100.00% <100.00%> (ø)
R/mf_map_pt.R 100.00% <100.00%> (ø)
R/mf_map_s.R 94.82% <100.00%> (ø)
R/mf_map_sc.R 89.70% <100.00%> (ø)
R/mf_map_t.R 98.11% <100.00%> (ø)
R/mf_map_utils.R 98.82% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0574658...b9ee9bb. Read the comment docs.

@rCarto
Copy link
Member

rCarto commented Apr 22, 2021

Hello Paul,

(...) so that palettes can be easily mapped with transparency (...)

The keyword here is easily. I try to keep the number of argument relatively small.
Wouldn't it be enough to use hcl.colors(..., alpha = x) directly in function calls?

library(maptiles)
library(mapsf)
#> Le chargement a nécessité le package : sf
#> Linking to GEOS 3.7.1, GDAL 3.1.2, PROJ 7.1.0
library(raster)
#> Le chargement a nécessité le package : sp
mtq <- mf_get_mtq()
# use EPSG:3857 crs
mtq <- st_transform(mtq, crs = 3857)
# get osm tiles
tiles <- get_tiles(mtq, zoom = 11)
# transform to raster
tiles <- brick(tiles)
# display tiles
mf_theme(mar = c(0,0,0,0), inner = T)
mf_raster(tiles)
mf_map(mtq, "MED", "choro",
      nbreaks = 6, pal = hcl.colors(6, "Blues", alpha = .7),
      add = TRUE, leg_frame = T)
mf_title("Median Income")
mf_credits(txt = get_credit("OpenStreetMap"))
mf_scale(5)

Created on 2021-04-22 by the reprex package (v2.0.0)

@PaulC91
Copy link
Contributor Author

PaulC91 commented Apr 22, 2021

Hi Timothée,

Thanks for you response!

I did try experiment with this and I realise now I was having issues because I was not setting nbreaks explicitly to match the number of colours I supplied manually. There will be a mismatch between legend colours and choro colours if the default calculated nbreaks is not the same as the number of colours supplied.

library(maptiles)
library(mapsf)
#> Loading required package: sf
#> Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
mtq <- mf_get_mtq()
mf_theme(mar = c(0,0,0,0), inner = T)
mf_choro(
  # default nbreaks is 6 in this case but if we supply a
  # manual palette with 10 colours there is a mismatch
  mtq, "MED", add = F, leg_frame = T,
  pal = hcl.colors(10, "Blues", alpha = 1, rev = T)
)
mf_title("Median Income")
mf_credits(txt = get_credit("OpenStreetMap"))
mf_scale(5)

Created on 2021-04-22 by the reprex package (v1.0.0)

So the nice benefit of adding the alpha argument for me would be to have the ability to simply supply a palette name and alpha level then have the function calculate the appropriate number of breaks and corresponding colours for me.

But I know it's not too much work to find the default nbreaks number myself and use this when supplying the palette so no worries if you don't want to include! :)

Best,
Paul

@rCarto rCarto merged commit dfeff40 into riatelab:master Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants