eliocamp / metR

Tools for Easier Analysis of Meteorological Fields
https://eliocamp.github.io/metR/
140 stars 22 forks source link

geom_streamline in function or loop #129

Closed TomBor closed 3 years ago

TomBor commented 3 years ago

geom_streamline is a great add to ggplot2. Thanks for making it.

I am unable to programmatically use aesthetic mappings proper to geomstreamline. For exemple use `aesoraes_stringwithdxordy`

Also, I would like to use aes(colour = ) with a discrete scale. Instead of a continuous wind speed, I have a discrete wind speed.

eliocamp commented 3 years ago

Hi! I'm not very familiar with aes_ and the like, but I don't see how geom_streamline() would not be compatible with that, since it's not doing anything weird with mappings. If you could post a minimal reproducible example, I could investigate the issue better.

Regarding discrete wind speed, you'd need to discretise it yourself. Use something like aes(colour = stat(cut(sqrt(dx^2 + dy^2)))).

TomBor commented 3 years ago

Sorry. I slice a very little part of my data to create the example below that works with the small text file in attachment wind_df_light.txt

In the dataframe, variables that starts with "ws_" are discrete wind speed in km/h that I would like to pass to the colour aesthetic. Thanks

library(tidyverse)
library(metR)

wind_df_light <- read_csv("wind_df_light.txt")

wind_plot <- function(u, v) {
  ggplot() +
    geom_streamline(data = wind_df_light,
                    aes(x = x, y = y, dx = u, dy = v,
                         colour = (sqrt(..dx..^2 + ..dy..^2) * 3.6),
                         size = ..step..,
                         alpha = ..step..),
                    res = 3, S = 10,
                    arrow = NULL, lineend = "round") +
    coord_equal() +
    scale_y_latitude() +
    scale_x_longitude()
}

u_list <- paste0("u_",1:6,"_")
v_list <- paste0("v_",1:6,"_")

map2(u_list, v_list, wind_plot)
eliocamp commented 3 years ago

Your discrete wind speed is defined in the regular grid, but geom_streamline draws lines that are derived from the vector field. You cannot use that information.

What you can do is to discretise the velocity at each integration step. Here's an example, which also demonstrates how to use aes_string()

library(ggplot2)
library(metR)

wind_df_light <- read.csv("~/Downloads/wind_df_light.txt")

wind_plot <- function(u, v) {

    ggplot() +
        geom_streamline(data = wind_df_light,
                        aes_string(x = "x", 
                                   y = "y", 
                                   dx = u ,
                                   dy = v,
                            colour = "cut((sqrt(..dx..^2 + ..dy..^2) * 3.6), breaks = c(10, 20, 40))",
                            size = "..step..",
                            alpha = "..step.."
                            ),
                        res = 3, S = 10,
                        arrow = NULL, lineend = "round") +
        coord_equal() +
        scale_y_latitude() +
        scale_x_longitude()
}

wind_plot("u_1_", "v_1_")

Created on 2020-11-25 by the reprex package (v0.3.0)

TomBor commented 3 years ago

Thanks you very much. It's more clear now.