ENMTools icon indicating copy to clipboard operation
ENMTools copied to clipboard

faster plotting of large models

Open plantarum opened this issue 4 years ago • 6 comments

Hi,

I've been using your package a lot over the past few weeks, and it's great!

However, it can be very slow to plot large models. I guess this is because you're converting rasters into huge sets of points, and then using ggplot, which is a little slow to begin with? I'm not sure, but I have a large model that takes 40-50 seconds to plot on a two-year old mid-range laptop.

I came up with a base R version that is much faster, and produces nearly the same layout. I've tested it on one of my models, and it drops the time from 50 seconds down to less than 1.

Here's the code:

library(viridis) # to match your color palette

plotTWS <- function(x, ...){
  plot(x$suitability, col = viridis(100, option = "B" ),
       xlab =  "Longitude", ylab = "Latitude",
       main = paste("Maxent model for", x$species.name),
       bty = 'l', box = FALSE)
  points(subset(vross.enm$analysis.df, presence == 1), pch = 21,
       bg = "white")
  points(vross.enm$test.data, pch = 21, bg = "green")
}

If that would be useful to you, I'm happy to clean it up and send you a pull request, either to replace your existing function, or as an alternative.

Best,

Tyler

plantarum avatar May 22 '20 04:05 plantarum

I really don't like base plots since they're hard to store in objects and modify after the fact. There may be something faster to do with ggplot, but @rdinnager was actually talking about revisiting these plots with some new packages that he says are quite fast.

Would you mind if I post your code on the ENMTools blog though (with attribution, obviously)?

danlwarren avatar May 22 '20 04:05 danlwarren

Sure, feel free!

I don't use ggplot2 enough to understand how or why you store and modify them, but I can appreciate that that's not possible at all with base graphics.

Best,

Tyler

plantarum avatar May 22 '20 04:05 plantarum

Am I correct in thinking that the vross.enm objects in your code should be replaced with x?

danlwarren avatar May 22 '20 10:05 danlwarren

yes, that must have slipped in during my tests

plantarum avatar May 22 '20 10:05 plantarum

How big a model are we talking here? What's the dimensions of the final suitability raster? Also what version of ggplot2 do you have installed? I'm going to do some benchmarking so this would be helpful information for that

rdinnager avatar May 22 '20 20:05 rdinnager

Here you go:

ggplot2 "/home/tws/R/x86_64-pc-linux-gnu-library/3.6" "3.3.0"

Suitability:
class : RasterLayer dimensions : 3678, 4967, 18268626 (nrow, ncol, ncell) resolution : 0.008333333, 0.008333333 (x, y) extent : -96.65833, -55.26667, 26.09167, 56.74167 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 source : memory names : layer values : 1.524336e-13, 0.9973617 (min, max)

plantarum avatar May 22 '20 23:05 plantarum