[R] Plotting confidence intervals with ggplot, in multiple facets.
@v|gro@@ @end|ng |rom ver|zon@net
Sun Jul 18 21:11:02 CEST 2021
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:
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:
geom_hline(yintercept = 0, color="red") +
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.
From: R-help <r-help-bounces using r-project.org> On Behalf Of Rolf Turner
Sent: Sunday, July 18, 2021 2:17 AM
To: r-help using 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
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
With eternal gratitude.
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
More information about the R-help