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
Dear Petr, This is not a ggplot2 solution, but you could just remove the NA rows and drop the levels of vzorek: met2 <- met[complete.cases(met), ] met2$vzorek <- droplevels(met2$vzorek) But I guess you already thought about that...! Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 30/04/2020 12:13, PIKAL Petr wrote:> 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 > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Yes, thank you Ivan. I used slightly different approach but with similar idea. I just wondered, if there is some clever hidden easy command or parameter inside ggplot environment which could drop panels without data. Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Ivan Calandra > Sent: Thursday, April 30, 2020 12:28 PM > To: r-help at r-project.org > Subject: Re: [R] ggplot get rid of unused/empty facets > > Dear Petr, > > This is not a ggplot2 solution, but you could just remove the NA rows and > drop the levels of vzorek: > met2 <- met[complete.cases(met), ] > met2$vzorek <- droplevels(met2$vzorek) > > But I guess you already thought about that...! > > Ivan > > -- > Dr. Ivan Calandra > TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS > Archaeological Research Centre and Museum for Human Behavioural > Evolution Schloss Monrepos > 56567 Neuwied, Germany > +49 (0) 2631 9772-243 > https://www.researchgate.net/profile/Ivan_Calandra > > On 30/04/2020 12:13, PIKAL Petr wrote: > > 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 > > > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.