Reputation: 5831
I want to draw a simple bar plot like so:
test <- data.frame(y=c(1,3,53,10,30,35,50), x=c(1:7))
barplot(test$y, names.arg=test$x)
My issue is that the y axis does not extend far enough if the maximum value is some "unpretty" number. What would be better is if the axis extended passed the maximum value and finished on some "pretty" value greater than the maximum.
Given a random dataset (i.e. this is going in a function), is there a simple way of doing this?
Upvotes: 2
Views: 7663
Reputation: 162321
You can use a combination of pretty()
and range()
to automatically pick nicer settings for ylim
:
test <- data.frame(y=c(1,3,53,10,30,35,50), x=c(1:7))
barplot(test$y, names.arg=test$x,
ylim = range(pretty(c(0, test$y))))
(Thanks to Gavin Simpson for pointing out that range(pretty(c(0, test$y)))
works just as well as range(pretty(c(0, max(test$y)))
)
Upvotes: 9