Home > Software engineering >  How to reduce the size of the legend box in R using DAAG library
How to reduce the size of the legend box in R using DAAG library

Time:10-13

I am new to R, and I am using an example to visualise 5-fold CV for the built-in dataset cars (it has two columns only: speed and distance).

For this purpose, I am taking advantage of DAAG library and its function CVlm. I installed the library using:

install.packages("DAAG")

Then, my code is as follows:

cvResults <- suppressWarnings(CVlm(data=cars, form.lm=dist ~ speed, m=5, dots=FALSE, seed=29, legend.pos="topleft", printit=FALSE, main="Small symbols are predicted values while bigger ones are actuals."))

This is what I get when I zoom in the displayed image:

As you can see, the legend box is very large and it overlaps more than half of the image.

I appreciate any help and feedback.

CodePudding user response:

There might be a bug, so I found a workaround by first specifying cex which is in base graphics:

character expansion factor relative to current par("cex"). Used for text, and provides the default for pt.cex.

After that, you can specify a custom legend. The tricky thing is with the exact colors. Here is a reproducible example:

install.packages("remotes")
remotes::install_gitlab('daagur/DAAG', build = TRUE, build_opts = c("--no-resave-data", "--no-manual"), dependencies=FALSE)
#> Skipping install of 'DAAG' from a gitlab remote, the SHA1 (ecc58275) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(DAAG)
CVlm(data=cars, 
     form.lm=dist ~ speed, 
     m=5, dots=FALSE, seed=29, 
     legend.pos="topleft", printit=FALSE,
     cex = 0.5,
     main="Small symbols are predicted values while bigger ones are actuals.")
#> Error in legend(x = legend.pos, legend = paste("Fold", 1:m, " "), pch = ptypes, : formal argument "cex" matched by multiple actual arguments
legend('topleft', legend = c('Fold 1', 'Fold 2', 'Fold 3', 'Fold 4', 'Fold 5'), 
       col = c('brown3', 'chartreuse3', 'darkviolet', 'black', 'deepskyblue3'),
       lty = 2:6,
       pch = 2:6,
       cex = 0.5)

Created on 2022-10-12 with reprex v2.0.2

  • Related