ctmm-initiative / ctmmweb

Web app for analyzing animal tracking data, built upon ctmm R package
http://biology.umd.edu/movement.html
GNU General Public License v3.0
32 stars 21 forks source link

creating maps for location, home range #35

Closed xhdong-umd closed 6 years ago

xhdong-umd commented 6 years ago

I'm working on maps for animal locations, home range/occurrence polygons.

@chfleming , the SpatialPolygonsDataFrame.UD returned the polygon coordinates as relative x,y values (Two-Point Equidistant projection?), not the absolute coordinates. There are reference coordinates in standard parallels of proj4string, so supposedly this should work, indeed the shapefiles(which have same structure) can be plotted in right location.

screen shot 2017-10-05 at 4 12 22 pm

However I'm having trouble plotting SpatialPolygonsDataFrame in right location.

screen shot 2017-10-05 at 4 12 37 pm

Should I reproject the data?

xhdong-umd commented 6 years ago

OK, I found I can reproject the data with spt_p <- spTransform(spt, CRS("+proj=longlat +datum=WGS84")). Though I need to make sure this is correct usage since these projections, datums, coordinate systems are quite complex.

chfleming commented 6 years ago

That looks right for long-lat. Generally, you want to get things into the same projection as the background map/raster. I have never found a way in R to plot rasters in a non-native projection, or even just to rotate a raster.

xhdong-umd commented 6 years ago

@chfleming @jmcalabrese I tried two datasets with Carto.com for various options. You can click each link to view them.

I think in general the Carto maps have better performance, and the Here satellite base map is also better.

It's still possible to use Carto maps in app, with these limitations:

xhdong-umd commented 6 years ago

I tried to add labels to home range polygons, but only the biggest range label is shown, i.e. all 3 ranges are showing same label. Simulated polygon data don't have this problem, and I didn't find any meaningful difference. I checked the hole status, row names of data slot etc.

I'll just skip the label for now, since it's relative straightforward based on context.

chfleming commented 6 years ago

I will look into it. I just finished fixing some contour labels/bugs and may have done something unintended.

xhdong-umd commented 6 years ago

@chfleming You don't need to spend extra time on this. This is probably not problem in ctmm side. I didn't update to the latest ctmm before running the code.

I can try to generate a minimal example to reproduce the problem, and submit bug report to leaflet if it's a bug.

xhdong-umd commented 6 years ago

I have submitted a issue to leaflet.

If there are some intersections between two individuals home range, what label should we show when mouse hover to that area? I think there is no perfect solution for this case.

There is a option to highlight polygons with mouse hovering, if we can highlight all polygons under mouse that should serve the purpose. However that option only highlight one polygon. Leaflet didn't consider these overlapping polygons case everywhere.

xhdong-umd commented 6 years ago

I'm not sure if we can plot the occurrence on the map. The occurrence data have a complex structure and I didn't find a method to draw them with leaflet.

xhdong-umd commented 6 years ago

We can work around the polygon label problem if we change the polygons order as bigger first, smaller second. However I think the label will not be perfect in general with lots of individuals overlapping. I'm inclined to just skip the label. User should have no problem to recognize which polygon is which without label.

xhdong-umd commented 6 years ago

I have updated the app with points map

screen shot 2017-11-01 at 3 47 24 pm screen shot 2017-11-01 at 4 01 12 pm
chfleming commented 6 years ago

When the colors are the same, I don't think extra labeling is necessary. Also, I think choosing one movement model will be, by far, the largest use case. So I would tune the user experience more for that purpose.

xhdong-umd commented 6 years ago

I can make the home range color same with the individual color, but two models for one individual will same color, is this OK?

chfleming commented 6 years ago

Probably not. Is it not possible to do something like the following: col=rainbow(n) for n unique models. Then partition col by individual animal and average each individual animal's partition of colors into one mean color for their data.

xhdong-umd commented 6 years ago

I have been using same color palette for animals through plots and summary tables. I think it's better to keep the animal location color consistent with other pages.

Maybe for multiple models of same individual, we can make each model color to be a small variation of brightness of location color. If there is only one model for one individual, it should be same color.

I'll try this and see how it works.

chfleming commented 6 years ago

For n individuals with colors col=rainbow(n), if an individual has m models, then you could make a dummy array of colors rainbow(n*m) and partition that dummy array into n dummy individuals with m model colors each. Then pull out the one individual's m length partition of model colors. Repeat for all individuals. This will keep the individual animal colors evenly spaced among all animals and the model colors evenly spaced within individuals.

xhdong-umd commented 6 years ago

The individual animal colors are used in about 10 plots and 2 tables. I think it's better to keep them consistent.

chfleming commented 6 years ago

Yes, that's what I am saying.

jmcalabrese commented 6 years ago

@xhdong-umd mapping doesn't work on my Linux system when I install the webapp with your script:

source("https://raw.githubusercontent.com/ctmm-initiative/ctmm-webapp/master/run.R")

I get: Error: could not find function "spTransform"

I thought the problem was that your installation script for the webapp was missing a dependency for the sp package. However, even after installing sp manually and restarting, I still get the same error.

xhdong-umd commented 6 years ago

Yes it's a dependency problem. sp has spTransform but it was actually came from rgdal. Though rgdal should be installed by ctmm. I will test again.

xhdong-umd commented 6 years ago

@jmcalabrese It should work now. I added sp and rgdal and tested with linux VM.

xhdong-umd commented 6 years ago

This is the color palette for 6 individuals (the plot usually have them with some transparency)

screen shot 2017-11-02 at 10 26 57 am

I call these color as base color. Now I need to create some variations from base color but still resemble the base color, so I'm not going to work in rgb space directly. Two alternatives are hcl and hsv.

hcl values don't always have [0,1] range depend on value combination, so it's not safe to interpolate in hcl space. I used hsv space instead.

The hsv value of first base color is as follows

h 0.01079137
s 0.56048387
v 0.97254902

I think it's best to only change v. v < 0.5 will be too dark, so I vary v of the base color in [0.5, 1.0] range depend on how many variations I need for one individual.

This is 6 variations from first base color plotted, the first box is the original base color.

screen shot 2017-11-02 at 10 27 19 am

One limit is the original base color have a v value of 0.97 so the variations will not be equal distance from the base color, like the case of 2 variations

screen shot 2017-11-02 at 10 27 28 am

I didn't find a simple way to overcome this. Though I think it's OK as long as the variations are separable but similar colors, since the original color is used in points, and the variations are used in home range polygons.

xhdong-umd commented 6 years ago

Here are the base colors of 13 models, duplicated base color means different models for same individual.

screen shot 2017-11-02 at 11 10 17 am

And these are variation colors for these models. Each duplicated color get some variations.

screen shot 2017-11-02 at 11 10 29 am
xhdong-umd commented 6 years ago

I have updated the repo app with new color theme.

I decided not to add legend for home range colors, since it could be too busy and difficult to separate for subtle differences. It's easier to find which is which by switching the layers on and off.

xhdong-umd commented 6 years ago

I have added heatmap and clustered map to repo.

xhdong-umd commented 6 years ago

For the syncing between heatmap and points map, my plan is

chfleming commented 6 years ago

I don't know if you have done it, but I would make the lowest AICc model the same color as the data and trend the variation such that the highest AICc model is the most dissimilar color.

xhdong-umd commented 6 years ago

The model color is only used in home range polygon color, not in the model summary table. In the map it's very difficult to separate the subtle difference because

screen shot 2017-11-03 at 3 14 04 pm

There are 4 models for Toni here but only 2 set polygons visible, with 2 models overlapped on each. So you are seeing the combined color instead of actual single color of polygons.

screen shot 2017-11-03 at 3 20 59 pm

I switched off two models to show 2 set of polygons without overlapping. Now you can see the real color of each model.

screen shot 2017-11-03 at 3 19 52 pm

Plus you said it's not a usual case to display multiple models of same individual in map, do we really expect user to catch the subtle color difference and map it to value? I think it will be quite challenging.

It will make more sense to use that color theme in the model summary table, however we are already using color grades in the 3 rows of one model, assign each model as a different shade of same color will make all the rows look very confusing.

NoonanM commented 6 years ago

I would suggest have a different line style for the 95% CIs on the contours.

xhdong-umd commented 6 years ago

The default contours have labels 95% high, 95% ML, 95% low. Which one do you want to use different line style? The 95% ML one? I think that one is always the middle sized polygon, right?

chfleming commented 6 years ago

The middle, point estimate should stand out the most, while the high and low estimates should be less emphasized. In ctmm's plot function I make them thinner and less opaque.

xhdong-umd commented 6 years ago

Yes I saw that. Do you want similar treatment, i.e. high and low thinner and less opaque? I think make it consistent with home range plot is a good idea.

This should apply to all levels, not just 95%, right? For example 95, 20, each should have the middle stand out.

NoonanM commented 6 years ago

Yes, in all cases the middle (ML) contour should stand out.

xhdong-umd commented 6 years ago

The satellite background can be difficult for home range polygons

screen shot 2017-11-03 at 4 23 41 pm

These are the 95%, 20% contour in terrain background

screen shot 2017-11-03 at 4 23 26 pm screen shot 2017-11-03 at 4 23 32 pm

I updated the repo app and you can have a test.

xhdong-umd commented 6 years ago

It seemed that Shiny doesn't support switching tabbed box in program. I can make tabbed panel like this and have the switching enabled, but it's not very good looking / consistent with other parts.

screen shot 2017-11-03 at 4 56 34 pm

Another option is just put heatmap in first tab as default, so the map should always render quickly and give an overview. Then user can decide whether to switch to points map.

Which option do you prefer?

xhdong-umd commented 6 years ago

@chfleming Previously I was hesitating on that color theme because the v value could be 0.64 ~ 1, so variation color could be brighter or darker. Thus if we assign the best model with base color, the other models may have color brighter or darker, it will be difficult to identify which one is same with base color in a series of similar shades.

How about we always use the brightest color for best model? The model color may not be same with base color, but brighter color models are always better than darker color models. I think it's much easier to identify which one is brighter, compare to which one is same with base color.

chfleming commented 6 years ago

That sounds fine to me.

xhdong-umd commented 6 years ago

@chfleming My model summary table was built on individual summary on each model so there is no AICc information. Though I think I can assume the models are always sorted with AICc in the model list, right?

On the other hand, should we include the AICc column in the model summary table?

chfleming commented 6 years ago

Yes and yes.

jmcalabrese commented 6 years ago

@xhdong-umd install from the script works now on my linux system. Not sure what the problem was, though. I definitely had rgdal installed. Thanks for fixing that.

jmcalabrese commented 6 years ago

@xhdong-umd I agree with the idea to remove the cluster map and instead focus on the point and heat maps. I've never liked the cluster map and agree that it is too busy and doesn't give a good sense of an individual's full distribution when zoomed out.

jmcalabrese commented 6 years ago

I agree with @NoonanM and @chfleming about the middle contour for the home ranges standing out more than the CI's.

jmcalabrese commented 6 years ago

@xhdong-umd I like the terrain background and agree that it is much easier to see what is going on relative to the satellite background. I would recommend making the terrain background the default with satellite background as an optional alternative.

jmcalabrese commented 6 years ago

@xhdong-umd I'm not clear on how your suggestion to have the heat map as the default tab would work. I the user zoomed to a preferred view with the heat map, would switching to the point map then preserve that zoom level?

And what about the case of smaller datasets with the performance of the point map would still be fine. Would it make sense then to always default to the heat map?

xhdong-umd commented 6 years ago

At first I couldn't make the switching heatmap tab work so I was thinking make it default. Now I've make it work so this suggestion can be ignored.

xhdong-umd commented 6 years ago

I finished on these changes:

Next I will make the home range color brightness map to the dAICc value, and sync the heatmap and points map bounds/zoom.

NoonanM commented 6 years ago

Maps currently look very good. I had a few thoughts that might appeal to users

xhdong-umd commented 6 years ago

It took me several hours to make the home range colors right.

I have updated the repo and now the home range with smaller dAICc are brighter.

@NoonanM

NoonanM commented 6 years ago

My thinking is that these plots are generally high quality, and informative. Many users might want to use the app to produce figures for publication (e.g., something like that attached figure). Being able to adjust the axes, and export the map as e.g. a png or pdf would enable some of that functionality.

gazelle_2 2

xhdong-umd commented 6 years ago

I did some simple search and didn't find support of axes in leaflet. The axes are often seen in the static map plot, but leaflet as a interactive live map seldom have this need from user.

There are ggmap which can generate static maps like the example above, but we didn't go that route because an interactive map should be much more useful.

User should be able to save the map itself and share it, which can be much more useful than a static plot. However I'm not aware of an option to export plot like above.

~Actually user will need to make a screenshot by himself/herself if a picture is needed. There is a leaflet.print plugin to save image from it, but that need another dependency of complex print modules, so I think we will need to rely on screenshot.~ There seemed to be a tool to save screenshot, I need to test it later. Update: it was very slow and require more dependencies, I think we will just let user to take screenshot.

xhdong-umd commented 6 years ago

Scale bar is added, also the repo and hosted app are updated.