Reputation: 1554
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?
Upvotes: 21
Views: 58195
Reputation: 2485
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:
Combining two answers together @rengis @vladiim
Upvotes: 4
Reputation: 1960
If you use ggplot you can use the scales package.
library(scales)
plot + scale_y_continuous(labels = percent)
Upvotes: 11
Reputation: 14413
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)
Upvotes: 22