Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a legend to a goodness of fit plot in R?

Tags:

plot

r

legend

I'm using goodfit from vcd package to produce goodness of fit plots.

I would like to add a legend stating the bars are the actual counts and the dots (connected by the line) are the fit using e.g. Poisson and ML.

legend does not work. How can I easily add a legend to this plot?

Thanks!

like image 534
David B Avatar asked Oct 30 '25 01:10

David B


1 Answers

The plot function for goodfit objects is using the grid graphics system (see ?rootogram and getAnywhere(rootogram.default)).

You have two options:

  1. use the rather limited grid.legend function (from package grid).
  2. embed a base graphics legend in the grid plot using the gridBase package.

Here is a simple example for the first option:

library("vcd")
dummy <- rnbinom(200, size=1.5, prob=0.8)
gf <- goodfit(dummy, type="nbinomial", method="MinChisq")
plot(gf)
pushViewport(viewport(x=unit(0.8, "npc"),
                      y=unit(0.8, "npc"),
                      width=stringWidth("Legend x"),
                      height=unit(6, "line"),
                      name="vp1"))
grid.legend(labels=c("Legend 1", "Legend 2"), pch=1:2)
popViewport()
like image 178
rcs Avatar answered Nov 01 '25 15:11

rcs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!