[R] Plotting confidence intervals with ggplot, in multiple facets.

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Tue Jul 20 03:39:42 CEST 2021



Your questions probably should go to a group focused on the ggplot package, not a general R group where many do not use it.


A little judicious searching like "R ggplot use greek letters in text" gets you some pointers that show how to do much more than Greek letters but more complex mathematical style equations and that many aspects of ggplot support it and you can use other functions like paste() to put more complex expressions together.


Similarly, your comments about wanting to have different scales showing in multiple plots using facet_grid() or perhaps facet_wrap() might answer your question as the manual page explains:



Are scales shared across all facets (the default, "fixed"), or do they vary across rows ("free_x"), columns ("free_y"), or both rows and columns ("free")?

So alter the line you use at the end to include a comma at the end of the arguments followed by “scale=…” as needed.


But yes, truly independent graphs placed in a grid, after loading the packages needed, gives even more options.


There are some very decent books and tutorials on many aspects of ggplot including some that are free and on-line. Here is an earlier edition of one:




Note especially this section on faceting:




I will now go silent on ggplot-related questions 😉



-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Rolf Turner
Sent: Monday, July 19, 2021 7:24 PM
To: r-help using r-project.org
Subject: Re: [R] Plotting confidence intervals with ggplot, in multiple facets.




Thanks to Jeff Newmiller, Rui Barradas and Avi Gross for their extremely helpful replies.  I have got both Jeff's and Rui's code to run.  I am currently experimenting with Avi's suggestion of producing multiple plots and then putting them together using plotgrid() or grid.arrange().  This idea seems to me to be most promising in terms of a desideratum that the y-axis scales/limits should be different on the two facets.  Also the y-axis labels.


And speaking of y-axis labels:  is it possible in ggplot() to get mathematical notation in axis labels, titles and possibly other annotation?  (In the manner of plotmath() in base R graphics.) Specifically I'd like to get the Greek letters alpha and beta in the y-axis labels.  In base R graphics I'd do something like ylab=expression(paste("bias in ",beta)) .  Is there an appropriate analogue in ggplot()?  (I think that I may have asked this question before, some time back, but have forgotten the answer.)






P.S.  The following is kind of apropos of nothing, but it might serve as a useful warning to others of a Trap for Young Players.  I nearly went mad (madder?) for a very long time when trying to get Rui's code to run.

I kept getting errors of the form:


> Error in source("scr.Rui") : scr.Rui:6:2: unexpected input

> 5: ggplot(eg, aes(Ndat, estimate)) +

> 6:   

>     ^


Took me an unconscionably long while to figure out what was going on.

I could not see why Jeff's code ran without problem, while Rui's (which was very similar) fell over.  Turns out the second character in the offending line is a non-printing character, the 160th member of the ASCII character set. (It can be produced using "\u00A0".)  Apparently this is a "non-breaking space". Whatever that means.  It does NOT get treated as white space in the usual way, and triggers the foregoing error.


Presumably this invisible character got introduced, into the code that Rui emailed, by one of the (many!) infuriating idiosyncrasies of Windoze.  Yet another reason, among the many millions of such, not to use Windoze.





Honorary Research Fellow

Department of Statistics

University of Auckland

Phone: +64-9-373-7599 ext. 88276



 <mailto:R-help using r-project.org> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see  <https://stat.ethz.ch/mailman/listinfo/r-help> https://stat.ethz.ch/mailman/listinfo/r-help

PLEASE do read the posting guide  <http://www.R-project.org/posting-guide.html> http://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list