Rolf Turner
2021-Jul-18 06:17 UTC
[R] Plotting confidence intervals with ggplot, in multiple facets.
I have need of creating a plot displaying confidence intervals (for the mean bias in parameter estimates) with one panel or facet for each of the two parameters in question. I can do this in base R graphics, but the result is not as aesthetically pleasing as I would like. I have attached an example graphic in the file "eg.pdf". I would like to try using ggplot2, but cannot get my head around the syntax. (Life is a struggle when one is old and senile!) I have been shown in the past how to produce a single-facet plot of such confidence intervals, basically using the geom_errorbar() function, but I cannot see how to produce multiple facets, depending on a "param" factor. I have thrashed around a bit but after succeeding in only confusing myself, I thought I would save wear and tear on my brain by asking this list. I'm sure the answer is pretty simple, but I'm just too stupid to see it. Can anyone give me a recipe for creating, with ggplot(), a graphic like unto that shown in "eg.pdf", but prettier? I have attached the data that were used to create "eg.pdf" in the form of a data frame, in a file called "egData.txt". This file was produced by dput() so read it in using dget("egData.txt"). With eternal gratitude. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 -------------- next part -------------- A non-text attachment was scrubbed... Name: eg.pdf Type: application/pdf Size: 5654 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210718/7726e136/attachment.pdf> -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: egData.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210718/7726e136/attachment.txt>
Jeff Newmiller
2021-Jul-18 06:47 UTC
[R] Plotting confidence intervals with ggplot, in multiple facets.
ggplot(dta,aes(x=Ndat,y=estimate, ymin=lower,ymax=upper))+ geom_point() + geom_errorbar(width=30) + facet_grid(param~1) + theme_minimal() Width parameter seems odd... play with it I suppose. For facets, you can also use facet_wrap(~param, ncol=1). ggplot is very much about the data and the aes mapping... the more you try to manually tweak it the more messed up it tends to get. On July 17, 2021 11:17:06 PM PDT, Rolf Turner <r.turner at auckland.ac.nz> wrote:> > >I have need of creating a plot displaying confidence intervals >(for the mean bias in parameter estimates) with one panel or facet >for each of the two parameters in question. > >I can do this in base R graphics, but the result is not as >aesthetically pleasing as I would like. I have attached an example >graphic in the file "eg.pdf". > >I would like to try using ggplot2, but cannot get my head around the >syntax. (Life is a struggle when one is old and senile!) I have been >shown in the past how to produce a single-facet plot of such confidence >intervals, basically using the geom_errorbar() function, but I cannot >see how to produce multiple facets, depending on a "param" factor. I >have thrashed around a bit but after succeeding in only confusing >myself, I thought I would save wear and tear on my brain by asking this >list. I'm sure the answer is pretty simple, but I'm just too stupid to >see it. > >Can anyone give me a recipe for creating, with ggplot(), a graphic like >unto that shown in "eg.pdf", but prettier? I have attached the data >that were used to create "eg.pdf" in the form of a data frame, in a >file called "egData.txt". This file was produced by dput() so read it >in using dget("egData.txt"). > >With eternal gratitude. > >cheers, > >Rolf Turner-- Sent from my phone. Please excuse my brevity.
Rui Barradas
2021-Jul-18 12:10 UTC
[R] Plotting confidence intervals with ggplot, in multiple facets.
Hello, Something like this? library(ggplot2) eg <- dget("data/egData.txt") ggplot(eg, aes(Ndat, estimate)) + ? geom_errorbar(aes(ymin = lower, ymax = upper), width = 20) + ? geom_point(colour = "slateblue", size = 1) + ? geom_hline(yintercept = 0, colour = "red") + ? facet_grid(param ~ .) + ? theme_bw() Hope this helps, Rui Barradas ?s 07:17 de 18/07/2021, Rolf Turner escreveu:> > I have need of creating a plot displaying confidence intervals > (for the mean bias in parameter estimates) with one panel or facet > for each of the two parameters in question. > > I can do this in base R graphics, but the result is not as > aesthetically pleasing as I would like. I have attached an example > graphic in the file "eg.pdf". > > I would like to try using ggplot2, but cannot get my head around the > syntax. (Life is a struggle when one is old and senile!) I have been > shown in the past how to produce a single-facet plot of such confidence > intervals, basically using the geom_errorbar() function, but I cannot > see how to produce multiple facets, depending on a "param" factor. I > have thrashed around a bit but after succeeding in only confusing > myself, I thought I would save wear and tear on my brain by asking this > list. I'm sure the answer is pretty simple, but I'm just too stupid to > see it. > > Can anyone give me a recipe for creating, with ggplot(), a graphic like > unto that shown in "eg.pdf", but prettier? I have attached the data > that were used to create "eg.pdf" in the form of a data frame, in a > file called "egData.txt". This file was produced by dput() so read it > in using dget("egData.txt"). > > With eternal gratitude. > > cheers, > > Rolf Turner > > > ______________________________________________ > 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.-- Este e-mail foi verificado em termos de v?rus pelo software antiv?rus Avast. https://www.avast.com/antivirus [[alternative HTML version deleted]]
Avi Gross
2021-Jul-18 19:11 UTC
[R] Plotting confidence intervals with ggplot, in multiple facets.
Rolf, Your example shows two plots with one above the other. If that is what you want, then a solution like the one Jeff provided using facet_grid() to separate data based on the parameter value. It also scales up if you add additional sets of data for gamma and delta up to a point. An alternative to consider if your ggplot wizardry has not kicked in, or if your need is to connect more diverse plots into a sort of collage, is to make multiple plots and save them as in: P1 <- ggplot(...) ... P2 <- ggplot(...) ... Each of these two or more plots can operate on whatever data you supply it. In your case, you would filter the rows that have the values you want. Then you can use one of many packages out there such as cowplot or gridextra to consolidate the parts as in: plot_grid(P1, P2) Or grid.arrange (P1, P2) These other functions vary in functionality but many allow you to adjust how many rows or columns you want or the relative sizes of the subplots and so on. Some allow you to create output into things like a PDF where parts spill over well into additional pages. I note one think Jeff did not replicate. If you want that in your output, use geom_hline as shown below: ggplot(dta,aes(x=Ndat,y=estimate, ymin=lower,ymax=upper))+ geom_point() + geom_errorbar(width=30) + geom_hline(yintercept = 0, color="red") + facet_grid(param~1) Personally, I sometimes adjust the limits to make a graph truly start at zero but in your case you can have error bars being displayed with parts below zero so showing where zero is graphically can be useful. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Rolf Turner Sent: Sunday, July 18, 2021 2:17 AM To: r-help at r-project.org Subject: [R] Plotting confidence intervals with ggplot, in multiple facets. I have need of creating a plot displaying confidence intervals (for the mean bias in parameter estimates) with one panel or facet for each of the two parameters in question. I can do this in base R graphics, but the result is not as aesthetically pleasing as I would like. I have attached an example graphic in the file "eg.pdf". I would like to try using ggplot2, but cannot get my head around the syntax. (Life is a struggle when one is old and senile!) I have been shown in the past how to produce a single-facet plot of such confidence intervals, basically using the geom_errorbar() function, but I cannot see how to produce multiple facets, depending on a "param" factor. I have thrashed around a bit but after succeeding in only confusing myself, I thought I would save wear and tear on my brain by asking this list. I'm sure the answer is pretty simple, but I'm just too stupid to see it. Can anyone give me a recipe for creating, with ggplot(), a graphic like unto that shown in "eg.pdf", but prettier? I have attached the data that were used to create "eg.pdf" in the form of a data frame, in a file called "egData.txt". This file was produced by dput() so read it in using dget("egData.txt"). With eternal gratitude. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276