3.7 Plotting Error Bars (lattice)

Honestly, lattice sucks at plotting error bars… Therefore, we will only explore one way of achieving this. In case you really want to explore this further, we refer you to StackOverflow and other R related forums and lists. You will possibly find a workable solution there, but we doubt that you will like it. As you will see in Section 3.8, error bars are much easier plotted using ggplot2.

Anyway, here is a solution built on a function called Dotplot() (not to be mistaken with lattice::dotplot()) from Hmisc (Harrell Jr and with contributions from Charles Dupont and many others 2017).

my_theme$dot.symbol$col <- "black"
my_theme$dot.symbol$cex <- 1.5
my_theme$plot.line$col <- "black"
my_theme$plot.line$lwd <- 1.5

dmod <- lm(price ~ cut, data = diamonds)
cuts <- data.frame(cut = unique(diamonds$cut), 
                   predict(dmod, data.frame(cut = unique(diamonds$cut)), 
                           se = TRUE)[c("fit", "se.fit")])

errbar_lattice <- Hmisc::Dotplot(cut ~ Cbind(fit, 
                                             fit + se.fit, 
                                             fit - se.fit),
                                 data = cuts, 
                                 par.settings = my_theme)

l_err <- errbar_lattice

print(l_err)
A **lattice** dotplot including error bars produced with `Hmisc::Dotplot()`.

Figure 3.26: A lattice dotplot including error bars produced with Hmisc::Dotplot().

References

Harrell Jr, Frank E, and with contributions from Charles Dupont and many others. 2017. Hmisc: Harrell Miscellaneous. https://CRAN.R-project.org/package=Hmisc.