[R] ggplot get rid of unused/empty facets

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Thu Apr 30 12:13:18 CEST 2020


Dear all

I seek some help how to remove unused facets in ggplot

Here is my code
p <- ggplot(met, aes(x=datum, y=lsp))
p+geom_point(size=5)+geom_line()+facet_grid(.~vzorek)

As you can see, there are some empty facets. Is there any reasonably simple
way how to automatically get rid of empty facets and adjust width of
remaining facets?

I could programmatically remove empty levels of vzorek before invoking
ggplot but before are start with such approach I would like to check if
there is some solution within ggplot.

Here are some toy data.

met <- structure(list(datum = structure(c(18282, 18282, 18282, 18290, 
18290, 18290, 18296, 18296, 18296, 18304, 18304, 18304, 18311, 
18311, 18311, 18317, 18317, 18317, 18317, 18324, 18324, 18324, 
18332, 18332, 18332, 18340, 18340, 18340, 18345, 18345, 18345, 
18352, 18352, 18369, 18369, 18369, 18375, 18375, 18375), class = "Date"), 
    vzorek = structure(c(9L, 8L, 2L, 5L, 9L, 6L, 1L, 7L, 8L, 
    9L, 5L, 1L, 2L, 7L, 8L, 5L, 2L, 1L, 7L, 9L, 7L, 5L, 4L, 2L, 
    1L, 3L, 8L, 5L, 9L, 2L, 1L, 7L, 8L, 9L, 8L, 2L, 1L, 7L, 5L
    ), .Label = c("a", "b", "c", "d", "e", "f", "g", "h", "i"
    ), class = "factor"), lsp = c(55.3, 54.68, 53.15, 54.55, 
    55.17, NA, NA, NA, 54.79, 55.27, 54.44, NA, 53.31, NA, 54.52, 
    54.35, 53.88, NA, NA, 55.21, NA, 54.39, NA, 52.95, NA, NA, 
    54.56, 54.39, 55, 53.2, NA, NA, 54.54, NA, 54.57, 53.31, 
    NA, NA, NA), asp = c(-1.63, -1.47, -1.19, -1.35, -1.62, NA, 
    NA, NA, -1.46, -1.65, -1.4, NA, -1.2, NA, -1.5, -1.36, -1.21, 
    NA, NA, -1.64, NA, -1.36, NA, -1.21, NA, NA, -1.5, -1.42, 
    -1.68, -1.2, NA, NA, -1.47, NA, -1.47, -1.22, NA, NA, NA), 
    bsp = c(-7.19, -5.65, -7.22, -5.31, -7.12, NA, NA, NA, -5.65, 
    -7.14, -5.34, NA, -7.16, NA, -5.68, -5.3, -7.14, NA, NA, 
    -7.14, NA, -5.34, NA, -7.17, NA, NA, -5.65, -5.34, -7.25, 
    -7.19, NA, NA, -5.65, NA, -5.66, -7.14, NA, NA, NA)), row.names = c(NA, 
-39L), class = "data.frame")
>

Cheers
Petr



More information about the R-help mailing list