[R] ggplot with sparse layout

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Jan 7 17:25:18 CET 2015


Dear Tom,

You can’t do that with facet_grid() or facet_wrap(). You could try combine several plots with grid.arrange().

p1 <-
  ggplot(subset(data, group == "a"),aes(x=x,y=y))+geom_point(aes(color=group))+facet_wrap(~id, nrow = 1) + scale_colour_discrete(drop = FALSE)
p2 <-
  ggplot(subset(data, group == "b"),aes(x=x,y=y))+geom_point(aes(color=group))+facet_wrap(~id, nrow = 1) + scale_colour_discrete(drop = FALSE)
p3 <-
  ggplot(subset(data, group == "c"),aes(x=x,y=y))+geom_point(aes(color=group))+facet_wrap(~id, nrow = 1)  + scale_colour_discrete(drop = FALSE)
library(gridExtra)
grid.arrange(p1, p2, p3)

If those solutions are not good enough, then you'll need to craft it in grid. Or look at the source code of ggplot2 and change that to get what you want.

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

Van: Tom Wright [mailto:tom op maladmin.com]
Verzonden: woensdag 7 januari 2015 15:43
Aan: ONKELINX, Thierry
CC: R. Help
Onderwerp: Re: [R] ggplot with sparse layout

Thanks, this is pretty good. Unfortunately I made an error in generating the sample dataframe, this code better represents the situation:
data<-data.frame(id=factor(rep(1:6,each=3)),
                  group=factor(rep(c('a','a','b','c','c','c'),each=3)),
                  x=runif(18,0,10),
                  y=rnorm(18,0,1))
I would like the first row to contain plots for id=1 & id=2, the second row plots for id=3 and the third row for id=4:6



On Tue, Jan 6, 2015 at 5:58 PM, ONKELINX, Thierry <Thierry.ONKELINX op inbo.be> wrote:
Dear Tom,

Does ggplot(data,aes(x=x,y=y))+geom_point(aes(color=group))+facet_wrap(~group + id) gives what you need?

Note that facet_grid by design aligns the subplots into rows and columns with the same level.

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey

________________________________________
Van: R-help [r-help-bounces op r-project.org] namens Tom Wright [tom op maladmin.com]
Verzonden: dinsdag 6 januari 2015 22:13
Aan: R. Help
Onderwerp: [R] ggplot with sparse layout

Hi,

I would like to plot the following data such that each plot represents the
data from a single id and the plots are grouped according to the group
variable.

data<-data.frame(id=factor(rep(1:6,each=3)),
 group=factor(c('a','a','b','c','c','c')),
 x=runif(18,0,10),
 y=rnorm(18,0,1))

The following ggplot nearly works but I would prefer to not plot the
missing combinations, i.e. each row will be a different length.

ggplot(data,aes(x=x,y=y))+geom_point(aes(color=group))+facet_grid(group~id)

Is this possible using ggplot or will I need to craft something using grid?

Thanks for any ideas.
Tom
        [[alternative HTML version deleted]]

______________________________________________
R-help op 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.
Disclaimer<https://drupal.inbo.be/nl/disclaimer-mailberichten-van-het-inbo>

Disclaimer<https://drupal.inbo.be/nl/disclaimer-mailberichten-van-het-inbo>


More information about the R-help mailing list