margins icon indicating copy to clipboard operation
margins copied to clipboard

Add legend to plot

Open mronkko opened this issue 5 years ago • 1 comments

Please specify whether your issue is about:

  • [ ] a possible bug
  • [ ] a question about package functionality
  • [ x] a suggested code or documentation change, improvement to the code, or feature request

The plot function does not currently produce a legend. This makes marginal prediction plots uninterpretable because the meaning of the different lines is not communicated in the plot.

Put your code here:

## load package
library("margins")

## code goes here
library(carData)

data(Prestige)

womenValues <- c(0, 25, 50, 75, 97.51)

m5 <- lm(income ~ education*women, data = Prestige)

mar5 <- margins(m5, variables = "education",
                at = list(women = womenValues))

# There seems to be a bug in margins. We will apply a workaround until it is 
# fixed https://github.com/leeper/margins/issues/147

attr(mar5, "at") <- select(attr(mar5, "at"),-index)

plot(mar5)

# Legend should be included

legend("topright",legend = womenValues,
       lwd = seq(2, 0.25, length.out = 5),
       title = "Share of women")```

mronkko avatar May 28 '20 08:05 mronkko

As in issue https://github.com/leeper/margins/issues/147, it feels like you need cplot, not plot. Not sure a legend would be useful there.

vincentarelbundock avatar May 29 '20 16:05 vincentarelbundock