Search code examples
rplotaxis-labels

Keep the default axis values but just turn them to percentages


I'm wondering how I can keep the default y-axis values by R in my plot but just turn them to percentages? Specifically, if we need to use axis() how should we specify "at =" in axis() to keep the default tickmarked values?

An example is here:

x = rbinom(10, 100, .7)
plot(x)

Here is what I thought might work but didn't:

plot(x, yaxt = "n")
tk = par("yaxp")
axis(2, at = seq(tk[1], tk[2], l = tk[3]), labels = paste0(seq(tk[1], tk[2], l = tk[3]), "%"))

Solution

  • This will do the job for you:

    plot(x, yaxt="n")
       axis(2, at=axTicks(2), labels=paste0("%", axTicks(2)))
    

    Below, you can see the result of plot(x) and solution above side by side:

       set.seed(123)
       x = rbinom(10, 100, .7)
    
       plot(x)
    
       plot(x, yaxt="n")
       axis(2, at=axTicks(2), labels=paste0("%", axTicks(2)))