Skip to content

Commit

Permalink
fixed a bug that caused ggR to plot wrong stuff when multiple layers …
Browse files Browse the repository at this point in the history
…where supplied
  • Loading branch information
KonstiDE committed Jan 23, 2024
1 parent 591c58d commit 74eaddb
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions R/ggR.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ ggR <- function(img, layer = 1, maxpixels = 500000, alpha = 1, hue = 1, sat = 0

img <- .toTerra(img)

layer <- unlist(.numBand(img, layer))
layer <- unlist(.numBand(img, layer))

multLayers <- if (length(layer)>1) TRUE else FALSE
Expand All @@ -109,14 +108,6 @@ ggR <- function(img, layer = 1, maxpixels = 500000, alpha = 1, hue = 1, sat = 0
call. = FALSE)
geom_raster <- TRUE
}
if(multLayers & !geom_raster & ggObj) {
warning("You asked for multiple layers but geom_raster is FALSE.",
"\ngeom_raster will be reset to TRUE",
"\nHint: in case you're looking for a grayscale and facetted plot, use:",
"\nggR(img, ..., geom_raster=TRUE)+scale_fill_gradientn(colors = grey.colors(100))",
call. = FALSE)
geom_raster <- TRUE
}
annotation <- !geom_raster

ex <- ext(img)
Expand All @@ -127,7 +118,7 @@ ggR <- function(img, layer = 1, maxpixels = 500000, alpha = 1, hue = 1, sat = 0
dimImg <- dim(xfort)

df <- lapply(names(xfort), function(layer) {
df <- data.frame(extract(xfort, seq_along(values(xfort)), xy = TRUE),
df <- data.frame(as.data.frame(xfort[[layer]], xy = TRUE),
layerName = factor(layer, levels = names(xfort)))
colnames(df) <- c("x", "y", "value", "layerName")
df
Expand Down Expand Up @@ -164,6 +155,8 @@ ggR <- function(img, layer = 1, maxpixels = 500000, alpha = 1, hue = 1, sat = 0
})
df <- do.call(rbind, df)
}

print(head(df))

x <- y <- value <- NULL
if(ggObj) {
Expand Down Expand Up @@ -202,3 +195,12 @@ ggR <- function(img, layer = 1, maxpixels = 500000, alpha = 1, hue = 1, sat = 0
}

}

test <- function(){
devtools::load_all()
ggR(lsat, 1:6, geom_raster=TRUE, stretch = "lin") +
scale_fill_gradientn(colors=grey.colors(100), guide = FALSE) +
theme(axis.text = element_text(size=5),
axis.text.y = element_text(angle=90),
axis.title=element_blank())
}

0 comments on commit 74eaddb

Please sign in to comment.