plot a subset for 4-way-interactions
qwang178 opened this issue · 3 comments
I am trying to plot a subset of the data for effects from an LME model with 4 way interactions, and found the legend doesn't work as desired. Taking the efc data as an example:
fit model with 4-way-interaction
library(ggeffects)
data(efc)
fit <- lm(neg_c_7 ~ c12hour * barthtot * c161sex * c172code, data = efc)
pr <- ggpredict(fit, c("c12hour[0,80,160]", "barthtot[30,50,70]", "c161sex", "c172code"))
plot(pr) + ggplot2::theme(legend.position = "bottom")
This shows the plots well with three panels. However, when I subset the object and plot them:
pr_subset<-pr[c(1,18,19,36,37,54)]
pr_subset
Predicted values of Negative impact with 7 items
barthtot = 30
c161sex = [1] Male
c172code = 1
c12hour | Predicted | 95% CI
0 | 15.75 | [13.05, 18.45]
80 | 13.71 | [12.13, 15.29]
160 | 11.67 | [ 8.92, 14.43]
barthtot = 70
c161sex = [2] Female
c172code = 3
c12hour | Predicted | 95% CI
0 | 11.46 | [10.72, 12.19]
80 | 13.20 | [12.21, 14.19]
160 | 14.95 | [12.80, 17.10]
plot(pr_subset)
This shows the two panels, but cannot correctly color/label the BARTHEL index. Is there a way to address the problem?
Could we please have a reproducible example? (Also, your question will be more readable if you edit it to put your code inside a code block, using triple-back-quotes (```) as delimiters ...)
Thanks for the quick response! The above example is from the efc data used in the package. The code line has been updated for clarity.
That's tricky. For four stratification characteristics (i.e. 4 focal terms), predictions are subset by panel
:
library(ggeffects)
data(efc)
fit <- lm(neg_c_7 ~ c12hour * barthtot * c161sex * c172code, data = efc)
pr <- ggpredict(fit, c("c12hour[0,80,160]", "barthtot[30,50,70]", "c161sex", "c172code"))
pr_subset <- pr[c(1, 18, 19, 36, 37, 54)]
panels <- unique(pr_subset$panel)
lapply(seq_along(panels), function(.i) {
as.data.frame(pr_subset[pr_subset$panel == panels[.i], ])
})
#> [[1]]
#> x predicted std.error conf.low conf.high group facet panel
#> 1 0 15.74648 1.3750517 13.047342 18.44562 30 [1] Male 1
#> 19 80 13.70928 0.8032895 12.132472 15.28609 30 [1] Male 1
#> 37 160 11.67208 1.4039684 8.916174 14.42798 30 [1] Male 1
#>
#> [[2]]
#> x predicted std.error conf.low conf.high group facet panel
#> 18 0 11.45581 0.3737412 10.72218 12.18944 70 [2] Female 3
#> 36 80 13.20333 0.5039332 12.21414 14.19252 70 [2] Female 3
#> 54 160 14.95084 1.0933392 12.80469 17.09700 70 [2] Female 3
The color aesthetic is based on the group
column, but each plot now has just "one group", which results in the wrong legend.
The good thing is we know when we have a subset data frame of predictions:
attributes(pr_subset)$is_subset
#> [1] TRUE
Based on this information, it might be possible to tweak the color-aes, though I'm not sure right now how to do it. Must look a bit more into ggplot2 to figure this out.