eliocamp / metR

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

geom_text_contour() and facet_grid() don't play well with each other #109

Closed freeseek closed 4 years ago

freeseek commented 4 years ago

If I used geom_text_contour() and facet_grid together, geom_text_contour() ends up plotting the same labels across all plots in the grid, even if geom_contour() plotted lines at different z levels. This really does not seem like the correct behavior. The following image shows an example of the problem: metR

eliocamp commented 4 years ago

I think that this is indeed expected behaviour. The contour versions in metR set global breaks instead of different breaks for each panel. This ensures comparability between all contour maps. Use metR::geom_contour2() instead of ggplot2::geom_contour().

freeseek commented 4 years ago

Hmmm. But with geom_contour2() I get this: metR Which is not what I want. I suppose there is no way to set local rather than global breaks then, something like scale = "free_z" so to speak.

eliocamp commented 4 years ago

I guess I could do that. Since the idea behind metR is to use meteorological variables in which the spacing between contour lines has physical meaning, I wanted to preserve it. But for other applications might be different. Could you post a minimal reproducible example to use as template?

freeseek commented 4 years ago

The following code with the attached table should reproduce the first figure I showed:

library(ggplot2)
library(metR)
df <- read.table('met.txt', header = FALSE)
ggplot(df, aes(x=V1, y=V2, z=V3, fill=V3)) +
 geom_raster(interpolate = TRUE) +
 geom_contour(size = .2) +
 geom_text_contour(stroke = 0.1, size = 2) +
 scale_x_continuous(NULL, breaks = NULL) +
 scale_y_continuous(NULL, breaks = NULL) +
 scale_fill_gradientn(guide = FALSE, colours=rainbow(5)) +
 coord_cartesian(expand = FALSE) +
 facet_grid(. ~ V4) +
 theme(strip.background = element_blank())

met.txt

eliocamp commented 4 years ago

Thanks!

eliocamp commented 4 years ago

Fixet it for you :)

Here's how it works:

library(ggplot2)
library(metR)

df <- read.table("https://github.com/eliocamp/metR/files/4140193/met.txt", header = FALSE)

ggplot(df, aes(x=V1, y=V2, z=V3)) +
    geom_contour_fill(global.breaks = FALSE) +
    geom_contour(size = .2) +
    geom_text_contour(stroke = 0.1, size = 2, global.breaks = FALSE) +
    facet_grid(. ~ V4) 

Created on 2020-02-07 by the reprex package (v0.3.0)

freeseek commented 4 years ago

Grazie Elio!