-1

I am building and customizing an ROC curve using ggplot in R. I am almost done except 2 challenges.

  1. The abline I am adding for some reason extends beyond 0 and 1. How to restrict this? The line should start from origin and go to (1,1)

  2. How do I show the decimal labels as percentages?

Code I wrote so far:

ggplot(roc, aes(x=fpr, ymin=0, ymax=tpr)) +
geom_ribbon(alpha=0.2) +
geom_line(aes(y=tpr)) +
ggtitle(paste0("ROC Curve, AUC=",auc)) +
xlab("Cumulative % Goods")+
ylab("Cumulative % Bads")+
geom_abline(intercept=0, slope = 1, color="red", linetype="dashed")+
theme_classic()

I also tried adding scale_y_continuous(labels='percent') but then it gives me an error "breaks and labels must have the same length"

How it looks like now

dsauce
  • 592
  • 2
  • 14
  • 36
  • 2
    please add some data so that we can use your code. – erc Jan 15 '16 at 09:40
  • 2
    In the future, ask _one_ question per post. Question 1 is probably a dupe of [this](http://stackoverflow.com/questions/13701347/force-the-origin-to-start-at-0-in-ggplot2-r). Question 2 is a dupe of [this](http://stackoverflow.com/questions/22821189/changing-a-continuous-scale-from-decimal-to-percents/). – Henrik Jan 15 '16 at 09:50

1 Answers1

1

I found the solution - I used "scales" library and added 2 elements to the plot. Full code below:

ggplot(roc, aes(x=fpr, ymin=0, ymax=tpr)) +
    geom_ribbon(alpha=0.1) +
    geom_line(aes(y=tpr)) +
    ggtitle(paste0("ROC Curve, AUC=",auc)) +
    xlab("Cumulative % Goods")+
    ylab("Cumulative % Bads")+
    geom_abline(intercept=0, slope = 1, color="red", linetype="dashed")+
    theme_classic()+
    scale_y_continuous(labels=percent, expand=c(0,0))+
    scale_x_continuous(labels=percent, expand=c(0,0))
dsauce
  • 592
  • 2
  • 14
  • 36