evalclass / precrec

An R library for accurate and fast calculations of Precision-Recall and ROC curves
https://evalclass.github.io/precrec
GNU General Public License v3.0
45 stars 5 forks source link

How to change the default ggplot2 theme produced by autoplot function? #7

Open Zefeng-Wu opened 6 years ago

Zefeng-Wu commented 6 years ago

Hi, there It is a nice R package. Now, I want to know how to change the plot theme generated by autoplot function, including the text font and size, etc. Moreover, i wand to add the auc score like "AUC=0.9" in the roc curve plot. Thanks

takayasaito commented 6 years ago

Hi,

The autoplot function of precrec does not return a ggplot object, but you can still use the fortify function to make a data frame for ggplot.

library(precrec)
library(ggplot2)

# Load a dataset with 10 positives and 10 negatives
data(P10N10)

# Generate an sscurve object that contains ROC and Precision-Recall curves
sscurves <- evalmod(scores = P10N10$scores, labels = P10N10$labels)

# Get AUCs
aucs <- auc(sscurves)
df <- data.frame(
  x = 0.5,
  y = 0.5,
  text = paste0("AUC: ",
                as.character(round(aucs[aucs$curvetypes == "ROC",]$aucs, 2)))
)

# Explicitly fortify sscurves
ssdf <- fortify(sscurves)

# Create an ROC plot
p_roc <- ggplot(subset(ssdf, curvetype == "ROC"), aes(x = x, y = y))
p_roc <- p_roc + theme_classic()
p_roc <- p_roc + geom_line()
p_roc <- p_roc + geom_label(data = df, aes(x, y, label = text))
p_roc <- p_roc + ggtitle("ROC")
p_roc <- p_roc + xlab("FPR")
p_roc <- p_roc + ylab("TPR")
p_roc

You can type help("fortify") to see more examples.

Hope this helps.