I have a raster object (link to file here) that plots contours 'wrong' in ggplot2 compared to base graphics.
Using ggplot2, the bottom area is missing the two 50% contour circles that are present in the base graphics plot. The total areas produced in the base graphics plot align to the calculated volume area sizes produced by the move::getVolumeUD(), which I'm inclined to trust/believe, and also align with the output generated using the fishtrack3d package: in terms of both calculated volume areas and UD contours.
library(magrittr)
library(ggplot2)
library(stars)
library(move)
test <- stars::read_stars(file.path("All_Rasters_Scaled_Weighted.asc")) %>% sf::st_set_crs(4326)
test %<>% starsExtra::trim2()
test <- as(test, "Raster")
test <- new(".UD", test)
is.na(test[[1]]) <- test[[1]] == 0
plot(test, xlab="location_long", ylab="location_lat", asp = 1)
contour(test, levels=c(.5, .95), col=c(6,2), add=TRUE, lwd=2)
That produces the following plot:
Obviously the asp ratio is different b/w contours and plot and looks ugly, but the key point is that it produces the 2 bottom 50% contour areas. This is like the plot produced by the fishtrack3d R package shown below:
Below is the ggplot code:
test <- stars::read_stars(file.path("All_Rasters_Scaled_Weighted.asc")) %>% sf::st_set_crs(4326)
contour1colour = "red" # colour for contour 1, typically 95%.
contour2colour = "orange"
legendtitle = "Percent UD Contours"
ggplot() +
geom_stars(data = test) +
ggplot2::geom_sf(data = stars::st_contour(x = test, contour_lines = TRUE, breaks = max(test[[1]], na.rm = TRUE) * 0.05), fill = NA, inherit.aes = FALSE,
ggplot2::aes(colour = "95% UD")) +
ggplot2::geom_sf(data = stars::st_contour(x = test, contour_lines = TRUE, breaks = max(test[[1]], na.rm = TRUE) * 0.5), fill = NA, inherit.aes = FALSE, ggplot2::aes(colour = "50% UD")) +
ggplot2::scale_colour_manual(name = legendtitle, values = c("50% UD" = contour2colour, "95% UD" = contour1colour))
which produces the following 'ugly' plot:
In this plot you can see that the bottom contours are absent. Given that the object is the same in both approaches, can anyone propose why the contour outputs are different?
Possibly relevant: in the ggplot2 approach we used 0.5 and 0.05 proportion of the object's max value as breaks values in st_contour(). In the base R approach, we set levels at 0.5 and 0.95. HOWEVER, the max value of this object is 0.008. According to the manual page for graphics::contour(), levels values seem like they should be absolute, rather than relative. Yet they behave as relative... or at least plot near identically to what we'd expect.
Thanks in advance for any help.