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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Aug 2 18:53:34 CEST 2021


Hello,

I'm glad it helped.
Here are a couple of ideas for theme.

1) From ?theme:

Theme inheritance
Theme elements inherit properties from other theme elements 
hierarchically. For example, axis.title.x.bottom inherits from 
axis.title.x which inherits from axis.title, which in turn inherits from 
text.


So there is no need for axis.title.x and axis.title.y (or axis.text) and 
  this

   theme_bw() +
   theme(axis.title.x=element_text(size=14),
         axis.title.y=element_text(size=14)) +
   theme(axis.text.x=element_text(size=14),
         axis.text.y=element_text(size=14)) +
   theme(panel.border=element_rect(linetype="solid",fill=NA),
         panel.grid.minor=element_blank(),
         panel.grid.major=element_blank())


can be simplified to this


   theme_bw() +
   theme(axis.title=element_text(size=14),
         axis.text=element_text(size=14)) +
   theme(panel.border=element_rect(linetype="solid",fill=NA),
         panel.grid=element_blank())


2) If you are using the same theme repeatedly, why not define a custom 
theme? It's as easy as


theme_custom <- function(){
   theme_bw() %+replace%    #replace elements we want to change
     theme(axis.title=element_text(size=14),
           axis.text=element_text(size=14),
           panel.border=element_rect(linetype="solid",fill=NA),
           panel.grid=element_blank())
}


(It's also possible to just copy&paste the two theme instructions in 
your code, I have rewritten them as one as a matter of habit.)
The plots would then become easier to read and if themes' rules change, 
the theme will be updated in one place only.


Part.a <- ggplot(cidf.a, aes(Ndat, estimate)) +
   geom_errorbar(aes(ymin = lower, ymax = upper), width = 50) +
   geom_point(size = 1) +
   geom_hline(yintercept = 0,col="red") +
   labs(x="",y=Ylab.a) +
   theme_custom()


And the same for Part.b.

Hope this helps,

Rui Barradas


Às 09:34 de 02/08/21, Rolf Turner escreveu:
> 
> 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
> 
> 
> ______________________________________________
> R-help using 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.
>



More information about the R-help mailing list