oscarperpinan / rastervis

The raster package defines classes and methods for spatial raster data access and manipulation. The rasterVis package complements raster providing a set of methods for enhanced visualization and interaction.
oscarperpinan.github.io/rastervis
GNU General Public License v3.0
84 stars 26 forks source link

Negative vectors in vectorplot? #16

Closed JoerivanEngelen closed 9 years ago

JoerivanEngelen commented 9 years ago

Dear Oscar,

I have found an issue with the legend and the colours of the raster dXY vector plot. When I use vectorplot(isField = dXY), the function creates areas with negative magnitudes of the vectors. (The colours indicate the magnitude of the vector, not the direction, right?) It would seem the most logical to me that the arrows indicate the direction and the colours the magnitude of the vector. As a vector cannot have a negative magnitude, the negative values seem wrong to me. A vector itsself cannot have a negative value, only its components I think if you would plot the absolute values of the now calculated magnitudes in the plot, this issue will be fixed.

It all seems like a small error in the coding to me, so if I come across as trying to school you; that is not my intention. It would by the way be nice to keep the pastel shade for the low velocities.

vectorplotdroog vectorplotnat

On a sidenote: Perhaps it would be a nice addition to the vector plot to include the physical quantity and the unit above the legend? I am thinking of the placement of short string like "v [m/s]" that can be defined by the user in the function, which is then placed above the legend by the function.

Kind regards, Joeri

oscarperpinan commented 9 years ago

Hello Joeri,

If region = TRUE, vectorplot displays the first layer of the Raster* as the background of the graphic (I have to fix the documentation because this argument isn't correctly explained). Thus, if isField = 'dXY' the background will be the horizontal component of the vector field. There are some examples in the help page of vectorplot regarding this issue. Maybe the third example should be the default behaviour (internally compute the slope and use it as the background), but I have to think about it.

On the other hand, it is not easy to add a title to the legend in levelplot. I will think about it. Meanwhile you can try this workaround: https://stat.ethz.ch/pipermail/r-help/2011-November/296493.html

Best,

Oscar. El 10/12/2014 14:56, "JoerivanEngelen" notifications@github.com escribió:

Dear Oscar,

I have found an issue with the legend and the colours of the raster dXY vector plot. When I use vectorplot(isField = dXY), the function creates areas with negative magnitudes of the vectors. (The colours indicate the magnitude of the vector, not the direction, right?) It would seem the most logical to me that the arrows indicate the direction and the colours the magnitude of the vector. As a vector cannot have a negative magnitude, the negative values seem wrong to me. A vector itsself cannot have a negative value, only its components I think if you would plot the absolute values of the now calculated magnitudes in the plot, this issue will be fixed.

It all seems like a small error in the coding to me, so if I come across as trying to school you; that is not my intention.

On a sidenote: Perhaps it would be a nice addition to the vector plot to include the physical quantity and the unit above the legend? I am thinking of the placement of short string like "v [m/s]" that can be defined by the user in the function, which is then placed above the legend by the function.

Kind regards, Joeri

— Reply to this email directly or view it on GitHub https://github.com/oscarperpinan/rastervis/issues/16.

JoerivanEngelen commented 9 years ago

Ah thanks a lot! I should have read more carefully. I now also finally understand what you mean with "slope" (a minor nitpick: I think the word "magnitude" is used more often than "slope". See for example the wikipedia page on vectors). When isField = 'dXY', it seems more intuitive to me, as a user, to use the magnitude (slope) as background.

I will try the workaround and otherwise I'll glue some titles manually above the legend in Word.

I now have another issue: I'd like to change the panel background colour to 'gray80'. I read somewhere here that in some functions of rasterVis you can do this by inserting panel.background(list(col='gray80'))) in the theme. This works when inserting it in the streamTheme of the streamplot function. But unfortunately it does not for the rasterTheme of the vectorplot function. Do you have a workaround for this?

Update: I got this to work by simply using the streamTheme for the vectorplot function

oscarperpinan commented 9 years ago

The streamTheme function includes this line:

  theme <- modifyList(theme, list(panel.background=panel.background))

where panel.background is list(col='gray20'). Thus, you can get what you want if you modify the result of rasterTheme:

myTheme <- rasterTheme()
myTheme <- modifyList(myTheme, list(panel.background = list(col = 'gray80'))
oscarperpinan commented 9 years ago

I now also finally understand what you mean with "slope" (a minor nitpick: I think the word "magnitude" is used more often than "slope". See for example the wikipedia page on vectors).

Here I am using slope and aspect because they are commonly used when working with Digital Elevation Models (for example, take a look at the bibliographic references of raster::terrain). Anyway, I will add additional comments in the help page of vectorplot to clarify these terms and relate them with the names used in vector algebra.

When isField = 'dXY', it seems more intuitive to me, as a user, to use the magnitude (slope) as background.

Yes, I agree. I will modify the code as soon as possible.

oscarperpinan commented 9 years ago

I will try the workaround and otherwise I'll glue some titles manually above the legend in Word.

You can also try this:

library(rasterVis)
library(grid)

f <- system.file("external/test.grd", package="raster")
r <- raster(f)
levelplot(r)
trellis.focus('legend', side = 'bottom', clip.off = TRUE, highlight = FALSE)
grid.text('[m/s]', 0, 0.3, just = 'right')
trellis.unfocus()
JoerivanEngelen commented 9 years ago

Thank you very much for all the responses. I think I will still used the first workaround, as it takes up less space and my supervisor apparently is not as picky on figures as I am. The figures I now obtained with the rasterVis package look way better than the figures that the hydrological modelling software (Hydrus2D/3D) exported, your quick responses also have been very helpful.