Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom plots using the effects package

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))
like image 259
Andy Avatar asked Dec 26 '22 00:12

Andy


1 Answers

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: enter image description here

like image 198
Jd Baba Avatar answered Jan 04 '23 20:01

Jd Baba