davidcarslaw / openairmaps

mapping functions to support openair
https://davidcarslaw.github.io/openairmaps/
GNU General Public License v3.0
21 stars 6 forks source link

[Question]: using ggplot2 for plotting the results from openair::trajCluster #76

Closed mooibroekd closed 1 month ago

mooibroekd commented 2 months ago

I would like to plot the clustered results using ggplot2 on a static map.

library(openair)

traj <- importTraj(site = "london", year = 2010)
#> Warning in load(con): cannot open URL
#> 'http://met-data.ricardo-aea.com/trajectories/london2010.RData': HTTP status
#> was '404 Not Found'
#> Warning in value[[3L]](cond): london2010does not exist - ignoring that one.

clust <- trajCluster(traj, method = "Angle", 
                     n.cluster = 6, 
                     col = "Set2",
                     map.cols = openColours("Paired", 10))
#> Error in `filter()`:
#> ℹ In argument: `!is.na(lat)`.
#> Caused by error:
#> ! object 'lat' not found

Created on 2024-08-12 with reprex v2.1.1

Unfortunately the reprex does not work due to importTraj not working.

mooibroekd commented 2 months ago

Plotting the data proved to be easy:

openairmaps::trajMapStatic(clust$data$results, 
                           colour = "cluster")

Next problem is how to increase the point size and line width without modifying the function itself. @jack-davison Do you know a way how to do this? I cannot use scale_linewidth_manual() or scale_size_manual() as these are not defined in the aes(). For color it does work:

openairmaps::trajMapStatic(clust$data$results, 
                           colour = "cluster") +
  ggplot2::scale_color_manual(values = c("C1" = "red"))
jack-davison commented 1 month ago

Hi Denis - sorry for the delay - unfortunately the function isn't currently set up to expose those options to the user.

To do so, I'd need to expose the various arguments of geom_path(), geom_point(), etc. - linewidth, size, etc. - to the user. It might be good if the user could map these to a variable - e.g. make the size / linewidth proportionate to some variable.

I'll give this a look.

jack-davison commented 1 month ago

Hi again @mooibroekd,

Can you give the dev version a go? Added the linewidth and size args:

trajMapStatic(traj_data, colour = "pm10", size = "height") +
  scale_color_gradientn(colors = openair::openColours(),
                        name = openair::quickText("PM10")) +
  guides(size = guide_none(),
         linewidth = guide_none()) +
  theme_bw()

image

Or can set them to a fixed val - i.e., trajMapStatic(data, "pm10", size = 2L, linewidth = 1L)