Search code examples
rggplot2yardstick

Modify figure sizes of `pr_curve` and `auc_curve` from R package yardstick


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.


Solution

  • 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)