Re: [R] ggplot facet label font size
Thanks for the lattice help Gabor and Deepayan. These snippets work well. I've only been using R a few months and it and the user community have exceeded my expectations. Best Regards, -Sam -Original Message- From: Deepayan Sarkar [mailto:[EMAIL PROTECTED] Sent: Sunday, August 06, 2006 1:18 AM To: Walker, Sam Cc: r-help@stat.math.ethz.ch; Gabor Grothendieck Subject: Re: [R] ggplot facet label font size On 8/3/06, Walker, Sam [EMAIL PROTECTED] wrote: This works OK, but there is some extra spacing between the panels, the top axis and the strip on the top, and the left labels and panel. How can I remove these extra spaces? This turns out to be surprisingly easy (surprising to me at least): # my.strip - function(which.given, which.panel, var.name, ...) { if (which.given == 1 which.panel[2] == 2) strip.default(1, which.panel[1], var.name = var.name[1], ...) } my.strip.left - function(which.given, which.panel, var.name, ..., horizontal) { if (which.given == 2 which.panel[1] == 1) strip.default(1, which.panel[2], var.name = var.name[2], horizontal = FALSE, ...) } histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.strip.text = list(lines = 0.6), par.settings = list(layout.heights = list(strip = c(0, 1)), layout.widths = list(strip.left = c(1, 0)), add.text = list(cex = 0.7))) # The trick of changing two-line strips to one-line strips is not obvious from the documentation (such as it is), it depends on the implementation of strips. HTH, Deepayan I've tried changing various layout.widths settings with no luck. It seems the spaces are calculated based on the number of conditioning variables, in this case 2 (sex+smoker). Thanks in advance... -Sam -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 02, 2006 6:04 PM To: Walker, Sam Cc: r-help@stat.math.ethz.ch Subject: Re: [R] ggplot facet label font size On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch mailing list 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. -- http://www.stat.wisc.edu/~deepayan/ __ R-help@stat.math.ethz.ch mailing list 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.
Re: [R] ggplot facet label font size
On 8/3/06, Walker, Sam [EMAIL PROTECTED] wrote: This works OK, but there is some extra spacing between the panels, the top axis and the strip on the top, and the left labels and panel. How can I remove these extra spaces? This turns out to be surprisingly easy (surprising to me at least): # my.strip - function(which.given, which.panel, var.name, ...) { if (which.given == 1 which.panel[2] == 2) strip.default(1, which.panel[1], var.name = var.name[1], ...) } my.strip.left - function(which.given, which.panel, var.name, ..., horizontal) { if (which.given == 2 which.panel[1] == 1) strip.default(1, which.panel[2], var.name = var.name[2], horizontal = FALSE, ...) } histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.strip.text = list(lines = 0.6), par.settings = list(layout.heights = list(strip = c(0, 1)), layout.widths = list(strip.left = c(1, 0)), add.text = list(cex = 0.7))) # The trick of changing two-line strips to one-line strips is not obvious from the documentation (such as it is), it depends on the implementation of strips. HTH, Deepayan I've tried changing various layout.widths settings with no luck. It seems the spaces are calculated based on the number of conditioning variables, in this case 2 (sex+smoker). Thanks in advance... -Sam -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 02, 2006 6:04 PM To: Walker, Sam Cc: r-help@stat.math.ethz.ch Subject: Re: [R] ggplot facet label font size On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch mailing list 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. -- http://www.stat.wisc.edu/~deepayan/ __ R-help@stat.math.ethz.ch mailing list 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.
Re: [R] ggplot facet label font size
With ggplot its possible to do this too but in that case it seems necessary to recurse through the grobs. Here we look for grobs that have a label component which contains a colon and grid.edit those changing the value of cex. Note that getNames() give a single grob named pretty and we start from that: # run code library(ggplot) library(grid) p - ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) recurse - function(x) { if (!is.null(x$label) regexpr(:, x$label) 0) grid.edit(x$name, gp = gpar(cex = 0.7)) for (ch in x$children) recurse(ch) } recurse(grid.get(pretty)) On 8/3/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: If you are willing to use grid then you could create only the sex factor in the left strips since its already in the desired position but when displaying it output a factor.level, i.e. label of A. (my.strip.left is modified from the prior post to do that.) Then after the plot is drawn, looping through all grobs looking for those with a label component of A producing a list of grob names, strip.left.names. We then mapply the real factor levels with those grobs editing them in reset.levels(), defined below. (I have used the fact, empirically determined that the stripts are produced in order of the factor levels.) Everything is the same as the last post except my.strip.left which has been modified and everything which comes after the call to histogram. Although this seems to work, maybe Deepayan or Paul can think of something slicker. library(ggplot) # data resides here library(lattice) library(grid) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., factor.levels, horizontal) if (which.given == 1 which.panel[1] == 1) strip.default(which.given, which.panel, factor.levels = LETTERS, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) is.strip.left - function(name) identical(grid.get(name)$label, A) strip.left.names - getNames()[sapply(getNames(), is.strip.left)] reset.levels - function(nam, lev) grid.edit(nam, label = lev) mapply(reset.levels , strip.left.names, levels(tips$smoker)) On 8/3/06, Walker, Sam [EMAIL PROTECTED] wrote: This works OK, but there is some extra spacing between the panels, the top axis and the strip on the top, and the left labels and panel. How can I remove these extra spaces? I've tried changing various layout.widths settings with no luck. It seems the spaces are calculated based on the number of conditioning variables, in this case 2 (sex+smoker). Thanks in advance... -Sam -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 02, 2006 6:04 PM To: Walker, Sam Cc: r-help@stat.math.ethz.ch Subject: Re: [R] ggplot facet label font size On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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
Re: [R] ggplot facet label font size
This works OK, but there is some extra spacing between the panels, the top axis and the strip on the top, and the left labels and panel. How can I remove these extra spaces? I've tried changing various layout.widths settings with no luck. It seems the spaces are calculated based on the number of conditioning variables, in this case 2 (sex+smoker). Thanks in advance... -Sam -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 02, 2006 6:04 PM To: Walker, Sam Cc: r-help@stat.math.ethz.ch Subject: Re: [R] ggplot facet label font size On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch mailing list 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.
Re: [R] ggplot facet label font size
Hi Sam, How do I change the font size in the facet labels along the edges of the plot? Unfortunately, you can't currently change the size of those fonts. However, it is on my todo list (as well as completely custom strip functions) and should be available in the near future. One thing you could do is have a look at ggopt, where you can at least change the strip text, if not the size. Regards, Hadley On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots, but the label font size is too large and exceeding the size of the label box. Thanks in advance... -Sam __ R-help@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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.
Re: [R] ggplot facet label font size
If you are willing to use grid then you could create only the sex factor in the left strips since its already in the desired position but when displaying it output a factor.level, i.e. label of A. (my.strip.left is modified from the prior post to do that.) Then after the plot is drawn, looping through all grobs looking for those with a label component of A producing a list of grob names, strip.left.names. We then mapply the real factor levels with those grobs editing them in reset.levels(), defined below. (I have used the fact, empirically determined that the stripts are produced in order of the factor levels.) Everything is the same as the last post except my.strip.left which has been modified and everything which comes after the call to histogram. Although this seems to work, maybe Deepayan or Paul can think of something slicker. library(ggplot) # data resides here library(lattice) library(grid) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., factor.levels, horizontal) if (which.given == 1 which.panel[1] == 1) strip.default(which.given, which.panel, factor.levels = LETTERS, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) is.strip.left - function(name) identical(grid.get(name)$label, A) strip.left.names - getNames()[sapply(getNames(), is.strip.left)] reset.levels - function(nam, lev) grid.edit(nam, label = lev) mapply(reset.levels , strip.left.names, levels(tips$smoker)) On 8/3/06, Walker, Sam [EMAIL PROTECTED] wrote: This works OK, but there is some extra spacing between the panels, the top axis and the strip on the top, and the left labels and panel. How can I remove these extra spaces? I've tried changing various layout.widths settings with no luck. It seems the spaces are calculated based on the number of conditioning variables, in this case 2 (sex+smoker). Thanks in advance... -Sam -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 02, 2006 6:04 PM To: Walker, Sam Cc: r-help@stat.math.ethz.ch Subject: Re: [R] ggplot facet label font size On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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.
Re: [R] ggplot facet label font size
Just sending this to you. One thing that might be easy to do yet give a lot of flexibility is to: 1. put meaningful names on the grobs. Even with just this it would be possible to do a getNames() in grid and then from inspection grid.edit the appropriate one(s). 2. create a routine that retrieves grobs so one does not have to use getNames with grep. trellis.focus and friends do this in lattice. Regards. On 8/3/06, hadley wickham [EMAIL PROTECTED] wrote: Hi Sam, How do I change the font size in the facet labels along the edges of the plot? Unfortunately, you can't currently change the size of those fonts. However, it is on my todo list (as well as completely custom strip functions) and should be available in the near future. One thing you could do is have a look at ggopt, where you can at least change the strip text, if not the size. Regards, Hadley On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots, but the label font size is too large and exceeding the size of the label box. Thanks in advance... -Sam __ R-help@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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@stat.math.ethz.ch mailing list 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] ggplot facet label font size
How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots, but the label font size is too large and exceeding the size of the label box. Thanks in advance... -Sam __ R-help@stat.math.ethz.ch mailing list 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.
Re: [R] ggplot facet label font size
On 8/2/06, Walker, Sam [EMAIL PROTECTED] wrote: How do I change the font size in the facet labels along the edges of the plot? For example (from the ggplot help file): p-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill)) gghistogram(p) In this plot, the facet labels are smoker: No, smoker: Yes, sex: Female, sex: Male. What command can I use to reduce the font size of these labels? In lattice terminology, cex is used to scale these strip labels. But I couldn't find the equivalent in ggplot. The reason I'm asking is I have a 9x7 array of plots which I've been plotting with lattice. I wanted to use ggplot because I like having the labels on the edge of the plots Note that lattice can do that by using custom strip functions: library(ggplot) # data resides here library(lattice) my.strip - function(which.given, which.panel, ...) if (which.given == 1 which.panel[2] == 2) strip.default(which.given, which.panel, ...) my.strip.left - function(which.given, which.panel, ..., horizontal) if (which.given == 2 which.panel[1] == 1) strip.default(which.given, which.panel, horizontal = FALSE, ...) histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip, strip.left = my.strip.left, par.settings = list(add.text = list(cex = 0.7))) __ R-help@stat.math.ethz.ch mailing list 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.