Coder Social home page Coder Social logo

Share plots about landscapetools HOT 9 CLOSED

ropensci avatar ropensci commented on May 30, 2024
Share plots

from landscapetools.

Comments (9)

marcosci avatar marcosci commented on May 30, 2024 1

The idea is brilliant! That very much fits the scope of the scaling around focal points ...

Do you want to implement it here in landacapetools? That would be a great addition.

from landscapetools.

Nowosad avatar Nowosad commented on May 30, 2024 1

I think we should stick to viridis as a default - rcartocolor or colorbrewer are good for ~12 categories, but they are not the best for more...
There should be an option to modify the default colors though...

from landscapetools.

mhesselbarth avatar mhesselbarth commented on May 30, 2024

I think this is a great idea because it includes different scales, which is always a good idea. Maybe it would be also interesting to not "only" include the share of each class, but different metrics.

@marcosci we discussed something similar at least as a vignette, right?

from landscapetools.

Nowosad avatar Nowosad commented on May 30, 2024

@mhesselbarth @marcosci please take a look at the code below. I think the code make sense, but there is still a work to be done related to the plot style (@marcosci ?).

After we decided on this and make it a part of the package, I will start working on something similar, but for landscape metrics.

library(landscapemetrics)
library(landscapetools)
library(raster)
#> Loading required package: sp
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.3.2, PROJ 4.9.3

# new functions -----------------------------------------------------------
# calculates shares for one buffer, but many points
share = function(buffer, x, y){
    df = as.data.frame(raster::extract(x = x,  y = y, buffer = buffer, df = TRUE))
    df = as.data.frame(table(df))
    df$buffer = buffer
    names(df) = c("id", "layer", "freq", "buffer")
    df
}

# calculates shares for many buffers and many points
extract_multibuffer = function(x, y, width, cutoff){
    buffers = seq(width, cutoff, width)
    df = do.call(rbind, lapply(buffers, share, x, y))
}

# prepare example data ----------------------------------------------------
data("augusta_nlcd")
augusta_nlcd2 = raster(augusta_nlcd)
augusta_nlcd2[] = augusta_nlcd[]

new_point = st_sf(geom = st_sfc(
    st_point(c(1265000, 1250000)),
    st_point(c(1255000, 1257000))))

plot(augusta_nlcd2)
plot(new_point, add = TRUE)

# example calculations ----------------------------------------------------
result = extract_multibuffer(augusta_nlcd2, new_point, width = 5000, cutoff = 50000)

# final plot --------------------------------------------------------------
library(ggplot2)
ggplot(result, aes(buffer, freq, group = layer, fill = layer)) +
    geom_area(position = "fill") +
    facet_wrap(~id) +
    expand_limits() +
    scale_y_continuous(name = NULL, expand = c(0, 0), labels = scales::percent) +
    scale_x_continuous(expand = c(0, 0)) +
    xlab("Distance (km)")

Created on 2019-03-02 by the reprex package (v0.2.1)

from landscapetools.

marcosci avatar marcosci commented on May 30, 2024

I just had a look at it - awesome. Looks solid, pretty nifty @Nowosad !
Do you want to keep the colors? We had pretty much viridis/cividis for everything else, which isn't that nice in categorical case. Maybe you have a better idea? Something from rcartocolor maybe.

from landscapetools.

marcosci avatar marcosci commented on May 30, 2024

The option would be just adding a new scale_fill, or?

from landscapetools.

marcosci avatar marcosci commented on May 30, 2024

I implemented the function now (https://github.com/ropensci/landscapetools/blob/master/R/show_shareplot.R), if there is something that can be improved let me know.

from landscapetools.

Nowosad avatar Nowosad commented on May 30, 2024

Neat! Thanks @marcosci

# remotes::install_github("ropensci/landscapetools")
library(landscapemetrics)
library(landscapetools)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.3.2, PROJ 4.9.3
library(raster)
#> Loading required package: sp

data("augusta_nlcd")
augusta_nlcd2 = raster(augusta_nlcd)
augusta_nlcd2[] = augusta_nlcd[]

nlcd_colors = data.frame(levels = c(0L, 11L, 12L, 21L, 22L, 23L, 24L, 31L, 41L, 42L, 43L, 51L, 52L, 71L, 72L, 73L, 74L, 81L, 82L, 90L, 95L), 
                          labels = c("nodata", "Open Water", "Perennial Ice/Snow", "Developed, Open Space", 
                                     "Developed, Low Intensity", "Developed, Medium Intensity", "Developed, High Intensity", 
                                     "Barren Land (Rock/Sand/Clay)", "Deciduous Forest", "Evergreen Forest", 
                                     "Mixed Forest", "Dwarf Scrub", "Shrub/Scrub", "Grassland/Herbaceous", 
                                     "Sedge/Herbaceous", "Lichens", "Moss", "Pasture/Hay", "Cultivated Crops", 
                                     "Woody Wetlands", "Emergent Herbaceous Wetlands"),
                          colors = c("#000000",  "#476BA0", "#D1DDF9", "#DDC9C9", "#D89382", "#ED0000", "#AA0000", 
                                     "#B2ADA3", "#68AA63", "#1C6330", "#B5C98E", "#A58C30", "#CCBA7C", 
                                     "#E2E2C1", "#C9C977", "#99C147", "#77AD93", "#DBD83D", "#AA7028", 
                                     "#BAD8EA", "#70A3BA"), 
                         stringsAsFactors = FALSE)

nlcd_colors = subset(nlcd_colors, levels %in% unique(augusta_nlcd2))

new_points = st_sf(geom = st_sfc(
    st_point(c(1265000, 1250000)),
    st_point(c(1255000, 1257000))))

show_shareplot(augusta_nlcd2, new_points, buffer_width = 5000, max_width = 50000) + 
    ggplot2::scale_fill_manual(values = nlcd_colors$colors)
#> Scale for 'fill' is already present. Adding another scale for 'fill',
#> which will replace the existing scale.

Created on 2019-04-29 by the reprex package (v0.2.1)

from landscapetools.

mhesselbarth avatar mhesselbarth commented on May 30, 2024

Looks good! 👍

from landscapetools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.