Search code examples
rplotdecimalseparatorsurvival

How to change the decimal separator in a ggsurvplot?


I would like to change the decimal separator of all the numbers (risk table, x and y axis, as well as p-value) in a Kaplan Meier curve from "." to "," (e.g. from 3.1415 to 3,1415). For the graph I use ggsurvplot from the survival package in R.

I found a post on here (Add comma separator to large numbers in risk table) describing how to add a ","-Separator for large numbers by changing the ggsurv object, but I don't know enough about this topic to modify it for my purpose.

Another post (R decimal comma instead of decimal point in ggplot scales::percent) I found was suggesting the usage of "geom_text", but this is not compatible with ggurvplot, only with ggplot as far as I know.

Is there any way how this can be done?


Solution

  • The answer you referenced already shows how this can be done. But instead of scales::comma you have to switch to the more general scales::number which allows to set the symbols used as decimal.mark and as big.mark. Also note that a ggsurvplot is simply a list where the ggplot is stored as an element named plot, i.e. to apply the second answer you reference you have to do something like ggsurv$plot + geom_text(....).

    Adapting the example and the answer by @AllanCameron to the more general case you could do:

    
    ggsurv$table$layers[[1]]$data$llabels <-
      scales::number(ggsurv$table$layers[[1]]$data$llabels, decimal.mark = ",", big.mark = ".", accuracy = .01)
    
    ggsurv
    

    enter image description here