简体   繁体   中英

Plot percentages on y-axis

I'm plotting a graph using this

plot(dates,returns)

I would like to have the returns expressed as percentages instead of numbers. 0.1 would become 10% . Also, the numbers on the y-axis appear tilted 90 degrees on the left. Is it possible to make them appear horizontally?

Here is one way using las=TRUE to turn the labels on the y-axis and axis() for the new y-axis with adjusted labels.

dates <-  1:10
returns <- runif(10)

plot(dates, returns, yaxt="n")
axis(2, at=pretty(returns), lab=pretty(returns) * 100, las=TRUE)

If you use ggplot you can use the scales package.

library(scales)
plot + scale_y_continuous(labels = percent)
library(scales)
dates <-  1:100
returns <- runif(100)
yticks_val <- pretty_breaks(n=5)(returns)
plot(dates, returns, yaxt="n")
axis(2, at=yticks_val, lab=percent(yticks_val))

Highlights:

  1. No need to explicitly add "%"
  2. Manually fix the number of y-ticks to be consistent with further plots. Here I chose 5.

看

Combining two answers together @rengis @vladiim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM