Bladeren bron

Adding ggplot2 version of simple.plotROCgg

Andrej 1 maand geleden
bovenliggende
commit
226bb3754c
4 gewijzigde bestanden met toevoegingen van 100 en 0 verwijderingen
  1. 2 0
      DESCRIPTION
  2. 1 0
      NAMESPACE
  3. 57 0
      R/roc.R
  4. 40 0
      man/simple.plotROCgg.Rd

+ 2 - 0
DESCRIPTION

@@ -13,3 +13,5 @@ RoxygenNote: 7.3.2
 Imports: 
     base,
     graphics
+Suggests: 
+    ggplot2

+ 1 - 0
NAMESPACE

@@ -7,6 +7,7 @@ export(setEventTime)
 export(simple.compute_roc_metrics)
 export(simple.getAUC)
 export(simple.plotROC)
+export(simple.plotROCgg)
 export(simple.roc)
 export(simple.sAUC)
 export(simple.sAUC2)

+ 57 - 0
R/roc.R

@@ -233,3 +233,60 @@ simple.plotROC<-function(df,var,col="black",x=0.65,y=0.1,unit="ml",precise=FALSE
    base::list(roc=roc,thr=roc_metrics$threshold,legend_text=legend_text)
 }
 
+#'Plot an assembly of ROCs
+#'
+#'@param df data frame
+#'@param vars vector of variables
+#'@param cols vector of color names
+#'@param x coordinate for legend
+#'@param y coordinate for legend
+#'@param unit unit for threshold in labels
+#'@param precise number of decimal places to use when reporting opt threshold, TRUE:2, FALSE:0
+#'@param target column that holds binary outcomes
+#'
+#'@return ggplot2 graphical object
+#'
+#'@export
+
+simple.plotROCgg<-function(df,vars,cols,x=0.7,y=0.3,unit="ml",precise="FALSE",target="alive"){
+   if (!requireNamespace('ggplot2',quiet=TRUE)){
+      print('ggplot2 not available. Use simple.plotROC function')
+      return(NULL)
+   }
+
+   #ggplot alternative
+   cvalues<-base::c()
+   colors_used<-base::c()
+
+   i=0
+   g<-ggplot2::ggplot()
+   for (var in vars) {
+      if (var %in% base::names(df)) {
+      # Pred izračunom ROC odstranimo vrstice z NA vrednostmi
+         df<-mapNA(df,var,0)
+         roc=simple.roc(df[,target],df[,var]) 
+#!! defuses evaluation of variable. https://rlang.r-lib.org/reference/topic-inject.html
+         col=cols[i]
+         roc_metrics <- simple.compute_roc_metrics(roc)
+         auc=simple.getAUC(roc)
+         sAUC=simple.sAUC(roc)
+         lab <- base::sprintf("[%s] AUC: %.2f (+- %.2f), OPT THR: %.2f",
+                           var, auc, sAUC, roc_metrics$threshold)
+
+         g<-g+ggplot2::geom_line(ggplot2::aes(x=!!roc$FPR,y=!!roc$TPR,color=!!lab))
+         aes_p=ggplot2::aes(x=!!roc_metrics$FPR,y=!!roc_metrics$TPR,color=!!lab)
+         g<-g+ggplot2::geom_point(aes_p,size=4,shape=1,show.legend=FALSE)
+         colors_used<-base::c(colors_used,col)
+         cvalues<-base::c(cvalues,lab) 
+         i=i+1
+      }
+     
+   }
+   base::names(colors_used)<-cvalues
+   g+ggplot2::xlab('1-specificity')+
+      ggplot2::ylab('sensitivity')+
+      ggplot2::scale_color_manual(name='Variables',values=colors_used)+
+      ggplot2::guides(color = ggplot2::guide_legend(position = "inside"))+
+      ggplot2::theme(legend.position.inside=base::c(x,y))
+
+}

+ 40 - 0
man/simple.plotROCgg.Rd

@@ -0,0 +1,40 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/roc.R
+\name{simple.plotROCgg}
+\alias{simple.plotROCgg}
+\title{Plot an assembly of ROCs}
+\usage{
+simple.plotROCgg(
+  df,
+  vars,
+  cols,
+  x = 0.7,
+  y = 0.3,
+  unit = "ml",
+  precise = "FALSE",
+  target = "alive"
+)
+}
+\arguments{
+\item{df}{data frame}
+
+\item{vars}{vector of variables}
+
+\item{cols}{vector of color names}
+
+\item{x}{coordinate for legend}
+
+\item{y}{coordinate for legend}
+
+\item{unit}{unit for threshold in labels}
+
+\item{precise}{number of decimal places to use when reporting opt threshold, TRUE:2, FALSE:0}
+
+\item{target}{column that holds binary outcomes}
+}
+\value{
+ggplot2 graphical object
+}
+\description{
+Plot an assembly of ROCs
+}