3

I'm trying to generate ROC curve and precision-recall curve using the library "yardstick". However, I could not find a way to modify the figure shape. Here's a toy example.

## Precision-recall curve
data.frame(true = as.factor(rep(c(0,1), 10)), 
           pred = runif(20)) %>% 
  pr_curve(truth = true, pred) %>% 
  autoplot()

## ROC curve
data.frame(true = as.factor(rep(c(0,1), 10)), 
           pred = runif(20)) %>% 
  roc_curve(truth = true, pred) %>% 
  autoplot()

When you run the codes, the generated figures look like below; enter image description here enter image description here

The top figure (ROC curve) is in a square form, while the bottom one (precision-recall curve) is a rectangle.

I've tried to

  • change width and height options in pdf function

  • change different options supported by ggplot2 (e.g. plot.margin using theme)

but could not find a good way to make two figures in the same shape.

How could I unify their shapes (or forms)?

Any comment will be very appreciated.

inmybrain
  • 386
  • 3
  • 16

1 Answers1

2

coord_fixed() from ggplot2 will do the trick. Note that you also need the adapt xlim and ylim if you want the plot area to be a square.

pr_curve(tmp1, truth = true, pred) %>% 
    autoplot() +
    coord_fixed(xlim = 0:1, ylim = 0:1)
hplieninger
  • 3,214
  • 27
  • 32
  • 1
    Thanks a lot! Based on your answer, I found that I can also adjust the relative size of x, y axes by `ratio` option in `coord_fixed`. – inmybrain Jul 26 '19 at 07:36