兩個選項:
library(ggplot2)
# evenly spaced, but not so pretty breaks
breaks.x <- with(mpg,round(seq(min(cty),max(cty),length.out=10),digits=1))
breaks.y <- with(mpg,round(seq(min(hwy),max(hwy),length.out=10),digits=1))
ggplot(mpg, aes(x=cty, y=hwy))+
geom_line() +
theme_bw()+
scale_x_continuous(breaks=breaks.x) + scale_y_continuous(breaks=breaks.y)
# pretty breaks, with extra grid lines at the limits
breaks.x <- with(mpg,pretty(seq(min(cty),max(cty),length.out=10)))
if (min(mpg$cty)>min(breaks.x)) breaks.x[1] <- min(mpg$cty)
if (max(mpg$cty)<max(breaks.x)) breaks.x[length(breaks.x)] <- max(mpg$cty)
breaks.y <- with(mpg,pretty(seq(min(hwy),max(hwy),length.out=10)))
if (min(mpg$hwy)>min(breaks.y)) breaks.y[1] <- min(mpg$hwy)
if (max(mpg$hwy)<max(breaks.y)) breaks.y[length(breaks.y)] <- max(mpg$hwy)
ggplot(mpg, aes(x=cty, y=hwy))+
geom_line() +
theme_bw()+
scale_x_continuous(breaks=breaks.x) + scale_y_continuous(breaks=breaks.y)
聽起來有點關係湯姆的問題在這裏:http://stackoverflow.com/questions/11427212/set-upper-限制在ggplot-to-include-label-greater-the-maximum-value – CMichael