Reputation: 4421
I am about to plot some variables (in a boxplot, but that doesn't matter) where I want to display the minimum and the maximum on the ordinate scale (y-axis).
require(ggplot2)
y_min <- min(PlantGrowth$weight)
y_max <- max(PlantGrowth$weight)
ggplot(PlantGrowth, aes(x=group, y=weight)) +
geom_boxplot() +
ylim(y_min, y_max)
Result:
I know, ylim()
is not made to directly edit the y-axis labels, but when setting another range, it accidentally works:
ggplot(PlantGrowth, aes(x=group, y=weight)) +
geom_boxplot() +
ylim(0, 8)
The straightforward solution is probably defining the ticks yourself:
ggplot(PlantGrowth, aes(x=group, y=weight)) +
geom_boxplot() +
scale_y_continuous(breaks=c(seq(y_min, y_max, 1.25), y_max))
which almost always results in a varying distance of the last and the next-to-last tick of the y-axis. It'd requires to experiment with the by-argument of seq()
until we a have an evenly ticked y-axis which includes the mininum and the maximum. Is there an elegant way? Not necessarily a ggplot2()
solution, but one that works on seq()
?
Upvotes: 0
Views: 1032
Reputation: 145785
You seem to be looking for the length.out
argument of seq
rather than the by
argument. That is,
scale_y_continuous(breaks = seq(y_min, y_max, length.out = 6))
You may want to wrap the sequence in round()
to avoid undue precision.
Upvotes: 4