Rolf Turner
2021-Jul-19 23:24 UTC
[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.) cheers, Rolf 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. R. -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Bill Dunlap
2021-Jul-20 00:21 UTC
[R] Plotting confidence intervals with ggplot, in multiple facets.
ggplot2::labs() interprets expressions as plotmath. E.g., data.frame(X=1:10,Y=(1:10)^2) %>% ggplot(aes(X,Y)) + geom_point() + labs(x = expression(beta), y = expression(beta^2)) -Bill On Mon, Jul 19, 2021 at 4:24 PM Rolf Turner <r.turner at auckland.ac.nz> wrote:> > > 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.) > > cheers, > > Rolf > > 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. > > R. > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Avi Gross
2021-Jul-20 01:39 UTC
[R] Plotting confidence intervals with ggplot, in multiple facets.
Rolf, 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: scales 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: https://ggplot2-book.org/ Note especially this section on faceting: https://ggplot2-book.org/facet.html I will now go silent on ggplot-related questions ? -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Rolf Turner Sent: Monday, July 19, 2021 7:24 PM To: r-help at 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.) cheers, Rolf 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. R. -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 ______________________________________________ <mailto:R-help at r-project.org> R-help at 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]]
Rolf Turner
2021-Aug-02 08:34 UTC
[R] Plotting confidence intervals with ggplot, in multiple facets.
I would like to tie off this thread (?!?!) by thanking Jeff Newmiller, Rui Barradas, Avi Gross and Bill Dunlap for their advice and insight. I have attached the code that I finally put together, on the basis of the aforementioned advice, in the file ciPlot.txt. I have also attached the necessary data set in the file egDat.txt. Just in case anyone is interested or in case someone else might benefit from seeing this code. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: ciPlot.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210802/82eed2b3/attachment.txt> -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: egDat.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210802/82eed2b3/attachment-0001.txt>