leeper / margins

An R Port of Stata's 'margins' Command
https://cloud.r-project.org/package=margins
Other
260 stars 40 forks source link

Add legend to plot #148

Open mronkko opened 4 years ago

mronkko commented 4 years ago

Please specify whether your issue is about:

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")```
vincentarelbundock commented 4 years ago

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.