Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't set limits for a graph with two y scales

I’m trying to plot a graph with two y scales. For this I use the following code

ggplot()+ 
    geom_line(data=plot_df_AIC, aes(x=x, y=AIC), color="red") +
    geom_line(data=plot_df_AUC, aes(x=x, y=AUC * 100), color="blue")+
    scale_y_continuous(sec.axis = sec_axis(~ ./100, name = "AUC"))+
  theme(
    axis.title.y = element_text(color = "red"),
    axis.text.y = element_text(color = "red"),
    axis.title.y.right = element_text(color = "blue"),
    axis.text.y.right = element_text(color = "blue")
  )

For the following data:

plot_df_AIC <- structure(list(x = 1:20, AIC = c(192.456332156948, 168.077664220646, 
151.620951957096, 147.718795436845, 137.444555845433, 100.40879221819, 
88.9138697158594, 71.0234955371967, 72.5073961740955, 74.8647755955335, 
75.3407718929883, 77.1002885963888, 70.2411723351313, 65.2276698782054, 
67.1077811158141, 67.4423951246628, 70.9441943080796, 59.719536949467, 
66.5031973889088, 68.8315706852903)), class = "data.frame", row.names = c(NA, 
-20L))
plot_df_AUC <- structure(list(x = 1:20, AUC = c(0.909483122312676, 0.932209889236273, 
0.946715923961602, 0.947250775396187, 0.956010866304684, 0.976934635048469, 
0.984366535772596, 0.98835498803627, 0.987203119638685, 0.985956953919869, 
0.986202696910042, 0.986506880115857, 0.982118157079483, 0.982189861532493, 
0.981637889799519, 0.977055762590389, 0.971319454724046, 0.965993970736366, 
0.953737877822623, 0.946781893644707)), class = "data.frame", row.names = c(NA, 
-20L))

And getting following output:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would like to change the AUC y limits. For this I am passing an argument limits = c(0.8, 1) into scale_y_continuous. But after that I get an error:

ERROR while rich displaying an object: Error in seq.default(range[1], range[2], length.out = self$detail): 'from' must be a finite number

Traceback:
1. tryCatch(withCallingHandlers({
 .     if (!mime %in% names(repr::mime2repr)) 
 .         stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
 .     rpr <- repr::mime2repr[[mime]](obj)
 .     if (is.null(rpr)) 
 .         return(NULL)
 .     prepare_content(is.raw(rpr), rpr)
 . }, error = error_handler), error = outer_handler)
2. tryCatchList(expr, classes, parentenv, handlers)
3. tryCatchOne(expr, names, parentenv, handlers[[1L]])
4. doTryCatch(return(expr), name, parentenv, handler)
5. withCallingHandlers({
 .     if (!mime %in% names(repr::mime2repr)) 
 .         stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
 .     rpr <- repr::mime2repr[[mime]](obj)
 .     if (is.null(rpr)) 
 .         return(NULL)
 .     prepare_content(is.raw(rpr), rpr)
 . }, error = error_handler)
6. repr::mime2repr[[mime]](obj)
7. repr_text.default(obj)
8. paste(capture.output(print(obj)), collapse = "\n")
9. capture.output(print(obj))
10. evalVis(expr)
11. withVisible(eval(expr, pf))
12. eval(expr, pf)
13. eval(expr, pf)
14. print(obj)
15. print.ggplot(obj)
16. ggplot_build(x)
17. ggplot_build.ggplot(x)
18. layout$setup_panel_params()
19. f(..., self = self)
20. Map(setup_panel_params, scales_x, scales_y)
21. mapply(FUN = f, ..., SIMPLIFY = FALSE)
22. (function (scale_x, scale_y) 
  . {
  .     self$coord$setup_panel_params(scale_x, scale_y, params = self$coord_params)
  . })(dots[[1L]][[1L]], dots[[2L]][[1L]])
23. self$coord$setup_panel_params(scale_x, scale_y, params = self$coord_params)
24. f(..., self = self)
25. view_scales_from_scale(scale_y, self$limits$y, self$expand)
26. view_scale_secondary(scale, limits, continuous_range)
27. scale$secondary.axis$break_info(continuous_range, scale)
28. f(..., self = self)
29. self$mono_test(scale)
30. f(..., self = self)
31. seq(range[1], range[2], length.out = self$detail)
32. seq.default(range[1], range[2], length.out = self$detail)
33. stop("'from' must be a finite number")

What should I do to correctly change the limits of this axis?

>Solution :

You can’t set limits for the secondary axis via the limits argument.

If you want a different scale or limits for the secondary axis you have to scale your data accordingly. My code below makes use of scales::rescale to do the (re-)scaling of your AUC data:

library(ggplot2)

from <- c(0.8, 1)
to <- range(plot_df_AIC$AIC)
plot_df_AUC$AUC <- scales::rescale(plot_df_AUC$AUC, from = from, to = to)

ggplot()+ 
  geom_line(data=plot_df_AIC, aes(x=x, y=AIC), color="red") +
  geom_line(data=plot_df_AUC, aes(x=x, y=AUC), color="blue")+
  scale_y_continuous(sec.axis = sec_axis(
    ~ scales::rescale(.x, to = from, from = to), name = "AUC"))+
  theme(
    axis.title.y = element_text(color = "red"),
    axis.text.y = element_text(color = "red"),
    axis.title.y.right = element_text(color = "blue"),
    axis.text.y.right = element_text(color = "blue")
  )

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading