eliocamp / metR

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

Issues with geom_arrow() when using geom_tile() #140

Closed dominicroye closed 1 year ago

dominicroye commented 3 years ago

Hi,

I have issues with geom_arrow() within geom_tile() and facet_grid() since my last update. I am using metR 0.9.1.9000 and tidyverse 1.3.0.

wind_santiago

Data example here

library(tidyverse)
library(metR)

load("data_wind.RData") # load data

col_p <- colorRampPalette(brewer.pal(9, "RdPu")) #color fill

df %>% 
  ggplot(aes(weekday,-week, fill = dcat)) +
  geom_tile(colour = "white", size = .4)  + 
  geom_text(aes(label = day, colour = text_col), size = 2.5,
            nudge_x = .35,
            nudge_y = -.25) +
  geom_arrow(aes(dx = u, dy = v, colour = text_col), 
             show.legend = FALSE,
             arrow.ends = "last",
             lineend = "round") +
  geom_point(data = filter(df, DIR == -1), 
             aes(weekday,-week),
             size = 1.1) +
  guides(fill = guide_colorsteps(barwidth = 25, barheight = .4,
                                 title.position = "top")) +
  scale_fill_manual(values = col_p(15),
                    na.value = "grey90", drop = FALSE) +
  scale_colour_manual(values =c("black", "white"), guide = FALSE) + 
  theme(aspect.ratio = 1/2,
        legend.position = "top",
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        panel.grid = element_blank(),
        axis.ticks = element_blank(),
        panel.background = element_blank(),
        strip.background = element_blank(),
        plot.caption =  element_text(hjust = 1, size = 8),
        legend.text = element_text(hjust = .5),
        strip.text = element_text(face = "bold", size = 15),
        panel.border = element_rect(colour = "grey", fill=NA, size=1),
        legend.title = element_text(size = 9, hjust = 1),
        plot.title = element_textbox_simple(hjust = .5, halign = 0.5, size = 26, face = "bold",
                                            margin = margin(0,0,0.5,0, unit = "cm")),
        plot.subtitle = element_text(hjust = .5, size = 16)) +
  facet_wrap(~month, nrow = 4, ncol = 3, scales = "free") +
  labs(title = title, 
       subtitle = "Velocidad de viento",
       caption = "Dominic Royé (@dr_xeo) | Datos: Meteogalicia",
       fill = "km/h")
dominicroye commented 3 years ago

I just downgraded metR to the commit "42c03f2cb3b8c688100e501ca79c62f53a3d24da" from 28 Nov, 2020 and it is working fine. See: https://xeo81.shinyapps.io/MeteoExtremosGalicia/

image

eliocamp commented 3 years ago

Thanks. That helps a lot.

eliocamp commented 3 years ago

Hi! Sorry it took me so long to look into this, especially because the end result might be anticlimactic. I think that the issue is that recent versions of metR don't add scale_mag() automatically with geom_arrow(). So you need to add it yourself.

Here's a minimal example using your data:

library(metR)
library(ggplot2)

url <- "https://www.dropbox.com/s/ovl51z8v6a3af0u/data_wind.RData?dl=1"
d <- `[`
file <- tempfile()

download.file(url, file)
load(file)

ggplot(df, aes(weekday,-as.numeric(week))) +
    geom_arrow(aes(dx = u, dy = v, colour = VEL), 
               show.legend = FALSE,
               arrow.ends = "last",
               lineend = "round") 


last_plot() +  scale_mag()

Created on 2021-08-06 by the reprex package (v2.0.0)