-
Notifications
You must be signed in to change notification settings - Fork 16
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
Share plots #17
Comments
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? |
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. |
@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) |
I just had a look at it - awesome. Looks solid, pretty nifty @Nowosad ! |
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... |
The option would be just adding a new scale_fill, or? |
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. |
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) |
Looks good! 👍 |
I created something that I called "a share plot" for one of my projects.
It show a share of each raster categories for some distance around a point of interest.
One example of this plot can be seen below.
@marcosci, @mhesselbarth, @mattfrit - do you think it could be useful in spatial ecology?
The text was updated successfully, but these errors were encountered: