Custom plots using the effects package

三世轮回 提交于 2019-12-06 04:36:28

问题


I try to customize the multiline graphs from the effects package.

Is there anyway to position the legend in the example below within the plotting area and not above the graph?

Alternatively: Does anyone know how to plot the results of the multiline regressions calculated by the effects package using ggplot2?

I appreciate any help.

Andy

Example:

library(effects)
data(Prestige)
mod5 <- lm(prestige ~ income*type + education, data=Prestige)
eff_cf <- effect("income*type", mod5)
print(plot(eff_cf, multiline=TRUE))

回答1:


This is how you plot effect object in ggplot

library(ggplot2)

## Change effect object to dataframe
eff_df <- data.frame(eff_cf)

## Plot ggplot with legend on the bottom
ggplot(eff_df)+geom_line(aes(income,fit,linetype=type))+theme_bw()+
  xlab("Income")+ylab("Prestige")+coord_cartesian(xlim=c(0,25000),ylim=c(30,110))+
  theme(legend.position="bottom")

You can change xlim and ylim depending on how you want to display your data.

The output is as follows:




回答2:


From ?xyplot you read :

Alternatively, the key can be positioned inside the plot region by specifying components x, y and corner. x and y determine the location of the corner of the key given by corner, which is usually one of c(0,0), c(1,0), c(1,1) and c(0,1), which denote the corners of the unit square.

and from ?plot.eff you read

key.args additional arguments to be passed to the key trellis argument to xyplot or densityplot, e.g., to position the key (legend) in the plotting region.

So for example you can do the following:

plot(eff_cf, multiline=TRUE,    
     key.args=list(x=0.2,y=0.9,corner=c(x=1, y=1)))




回答3:


Based on Ruben's answer, you can try following:

library(sjPlot)
sjp.int(mod5, type = "eff", swapPredictors = T)

which will reproduce the plot with ggplot, and sjp.int also returns the plot object for further customization. However, you can also set certain legend-parameters with the sjPlot-package:

sjp.setTheme(legend.pos = "bottom right", 
             legend.inside = T)
sjp.int(mod5, type = "eff", swapPredictors = T)

which gives you following plot:

See sjPlot-manual for examples on how to customize plot-appearance and legend-position/size etc.

For plotting estimates of your model as forest plot, or marginal effects of all model terms, see ?sjp.lm in the sjPlot-package, or you may even try out the latest features in my package from GitHub.




回答4:


@Tom Wenseleers

You can use sjPlot::sjp.int with type='eff' for this.

However, it won't give you rug plots and no raw data points yet either.

mod5 <- lm(prestige ~ type * income + education, data=Prestige)
library(sjPlot)
sjp.int(mod5,showCI = T, type = 'eff')

There's an argument partial.residuals = T to the effect() function. This gives you fitted values, partial.residuals.raw and partial.residuals.adjusted. I suppose you could merge that data on the original dataset and then plot smooths by group, but I ran into some difficulties early on (e.g. na.action=na.exclude is not respected).



来源:https://stackoverflow.com/questions/17658933/custom-plots-using-the-effects-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!