gpt4 book ai didi

r - 使用效果包自定义绘图

转载 作者:行者123 更新时间:2023-12-04 22:47:38 24 4
gpt4 key购买 nike

我尝试从效果包中自定义多线图。

无论如何将下面示例中的图例定位在绘图区域内而不是图形上方?

或者:有谁知道如何使用 ggplot2 绘制由 effects 包计算的多线回归的结果?

我很感激任何帮助。

安迪

示例:

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

最佳答案

这就是你在 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")

您可以更改 xlimylim取决于您希望如何显示数据。

输出如下:
enter image description here

关于r - 使用效果包自定义绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17658933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com