Open FrogBoss74 opened 4 years ago
Can you somehow share this data or produce a reproducible example using some openly available data? Everything else is just guessing on my side...
I could not reproduce the second colour issue. I ll share data if i manage to reproduce. The main point is about control of format for the legend.
library(sf)
study_area = st_as_sf(data.frame(geom=c("POLYGON((0 0, 0 500, 500 500, 500 0, 0 0))"),
stringsAsFactors = F), wkt="geom", crs=3857)
gr = st_sf(grid = st_make_grid(study_area, n = dim(volcano), square =T, what="centers"))%>%
rownames_to_column(var="gr.id")%>%
mutate(risk = 10^(5*log10(c(volcano)-min(volcano))-14))%>%filter(risk < 1e-3)
jetc <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
mc <-pretty(c(-7,-3))
# First issue for legend scientific format
gr %>% mapview(zcol="risk", # Variable to plot
legend=T, cex=1.5, # cosmetics
col.regions=jetc(4), at=rev(10^mc), alpha=0.2, layer.name="risk")
Desired output for instance using sci notation (here I workaround the issue overlaying iso contours on top), better using 10^x format but i could not find any workaround for that.
This should now be partially solved. At least we can now pass any valid argument to leaflet::addLegend
via ...
Means, we can now at least show more than 3 digits in the legend:
gr %>% mapview(zcol="risk", # Variable to plot
legend=T, cex=1.5, # cosmetics
col.regions=jetc(4), at=rev(10^mc), alpha=0.2, layer.name="risk",
labFormat = leaflet::labelFormat(digits = 6))
Honestly, I have no clue how to realise the scientific notation. If it's possible in leaflet, it should be possible here, but I couldn't find anything on how to specify it. Maybe using format
somehow?
Sorry for late follow up and thanks for highlighting the labFormat route.
I have done some further digging at it appears that leaflet::labelFormat
calls an in internal function formatNum
which has a scientific=F
parameter. Somehow leaflet::labelFormat
does not allow to set that parameter.
This would fix the issue, though still need to set digits to high number say 20:
labelFormatCustom = function (prefix = "", suffix = "", between = " – ",
digits = 3, big.mark = ",", transform = identity, scientific=T)
{
formatNum <- function(x) {
format(round(transform(x), digits), trim = TRUE, scientific = scientific,#TRUE,
big.mark = big.mark)
}
function(type, ...) {
switch(type, numeric = (function(cuts) {
paste0(prefix, formatNum(cuts), suffix)
})(...), bin = (function(cuts) {
n <- length(cuts)
paste0(prefix, formatNum(cuts[-n]), between, formatNum(cuts[-1]),
suffix)
})(...), quantile = (function(cuts, p) {
n <- length(cuts)
p <- paste0(round(p * 100), "%")
cuts <- paste0(formatNum(cuts[-n]), between, formatNum(cuts[-1]))
paste0("<span title=\"", cuts, "\">",
prefix, p[-n], between, p[-1], suffix, "</span>")
})(...), factor = (function(cuts) {
paste0(prefix, as.character(transform(cuts)), suffix)
})(...))
}
}
gr %>% mapview(zcol="risk", # Variable to plot
legend=T, cex=1.5, # cosmetics
col.regions=jetc(4), at=rev(10^mc), alpha=0.2, layer.name="risk",
labFormat = labelFormatCustom(digits = 20, scientific=T))
If that solves the issue, I think you should open an issue with leaflet. mapview will then allow you set it too.
Is there a way to display legend when values plotted are relatively low say 1e-9 to 1e-3? ideally, I'd like to display region colour legends in range bands eg (1e-3, 1e-4], (1e-4, 1e-5], (1e-5, 1e-6], etc. log10 scale or not. Right now it shows 0 as values are truncated below 0.001. The data looks like a grid of grid points with each point having a value ranging 1e-9 to 1e-3. For presentation purpose I don't want to show the log10 values in the legend. More control on the legend display will be useful. This is the first issue.
the data is plotted as follows:
The second issue comes as I try a workaround to this by overlaying isocontours on top of the data for which I can better control the legend and colour. I have tried to generate iso countours add one more column called label with this category however if I use this method to plot the label column instead I am not able to match the legend colour and the geometry object colours (ie it is hard to match the legend colour with the plotted colours). I get matching colours with the colour regions only if I plot the level column. If I plot the range column I strangely have no matching colour between the legend colour and the countour colours.
The iso contour data looks like:
the following generates a mismatch in colours between legends and contours
the following generates a colour matching but I am back to displaying log10 values which is not very aestetic