R/plot.R

Defines functions plot.linreg

Documented in plot.linreg

#' Plot LM Shape
#' 
#' A plot function for the linear regression model "linreg" using ggplo2, with the LIU loggo.
#' 
#' @param x An object of class "linreg".
#' @param ... other arguments. 
#' @param col Set color to the residuals default is black. 
#' 
#' @return Two plots, Residuals vs Fitted and Scale-Location.
#' 
#' @export


plot.linreg<-function(x,col="black",...){
  df<-base::data.frame(cbind(x$fitted_values,x$Residuals,base::sqrt(base::abs((x$Residuals-base::mean(x$Residuals)))/stats::sd(x$Residuals))))
  
  a<-ggplot2::ggplot(data=df, ggplot2::aes( x=df[,1],y=df[,2]))+
    ggplot2::geom_point(color=col)+
    ggplot2::geom_smooth(method = "loess" ,color="red",se=F)+
    ggplot2::ggtitle("Residuals vs Fitted")+
    ggplot2::scale_y_continuous()+
    ggplot2::geom_hline(yintercept = 0,linetype="dashed",col="black")+
    ggplot2::labs(x="Fitted values",y="Residuals")+
    ggplot2::theme(panel.background = ggplot2::element_rect(fill="lightblue",
                                                   color="lightblue"),
                   panel.border = ggplot2::element_blank(),
                   panel.grid.major = ggplot2::element_blank(),
                   panel.grid.minor = ggplot2::element_blank(),
                   axis.line = ggplot2::element_line(colour = "blue"))
  
  b<-ggplot2::ggplot(data=df, ggplot2::aes( x=df[,1],y=df[,3]))+
    ggplot2::geom_point(color=col)+
    ggplot2::geom_smooth(method = "loess", formula= "base::jitter(y,factor=1) ~ base::jitter(x,factor=1)",span=1,color="red",se=F)+
    ggplot2::ggtitle("Scale-Location")+
    ggplot2::geom_hline(yintercept = 0,linetype="dashed",col="black")+
    ggplot2::labs(x="Fitted values",y="|Standardized residuals|")+
    ggplot2::theme(panel.background = ggplot2::element_rect(fill="lightblue",
                                                   color="lightblue"),
                   panel.border = ggplot2::element_blank(),
                   panel.grid.major = ggplot2::element_blank(),
                   panel.grid.minor = ggplot2::element_blank(),
                   axis.line = ggplot2::element_line(colour = "blue"))
  
  gridExtra::grid.arrange(a,b)
  loggo <-magick::image_read("https://raw.githubusercontent.com/harjew/lab4G3/master/logo/liu_logo.png")
  scale_loggo<-magick::image_scale(loggo,"200")
  grid::grid.raster(scale_loggo, x = 1, y = 1, just = c('right', 'top'), width = grid::unit(2, 'inches'))
}
harjew/lab4G3 documentation built on Nov. 4, 2019, 1:27 p.m.