ylim specification when scale_format set to percent

947 views
Skip to first unread message

Rob James

unread,
Sep 16, 2010, 7:25:17 PM9/16/10
to ggplot2
Can one/how might one specify limits for ylim when scale_y_continuous
specifies (... formatter="percent")?


Brandon Hurr

unread,
Sep 17, 2010, 4:19:33 AM9/17/10
to Rob James, ggplot2
Rob,
I'm not sure what exactly you're after, but the following code seems
to allow me to set the percentages from 0 to 100 by specifying the
limits as between 0 and 1...

asdf<-runif(5)
ghjk<-c("A", "B", "C", "D", "E")
A1<-as.data.frame(cbind(asdf, ghjk))
A1[[1]]<-as.numeric(as.character(A1[[1]]))

ggplot(data=A1)+
geom_point(aes(x=ghjk, y=asdf))+
scale_y_continuous(limits=c(0, 1), formatter="percent")

Is that what you're looking for?

Brandon

Rob James

unread,
Sep 24, 2010, 9:49:26 PM9/24/10
to Brandon Hurr
Brandon,

Thanks for the ideas. Sorry for the delay in following up. This just
documents for completeness.

Here's was the problem and solution..(rather obvious when you come back
at it):

x <-abs(rnorm(100))
df <-data.frame(x)
df$rate <- df$x/100
ggplot(df)+geom_point(aes(x=x,
y=rate))+scale_y_continuous(formatter='percent')

Works fine.

In my dataset, range was 0-3%... Say I wanted 0-10% on the Y axis.

ggplot(df)+geom_point(aes(x=x,
y=rate))+scale_y_continuous(formatter='percent')+ylim(0,0.1)

Does not do as expected, and forces the y scale back to decimal notation.

ggplot(df)+geom_point(aes(x=x,
y=rate))+scale_y_continuous(formatter='percent', limits=c(0,0.1))

works fine.

The ylim documentation might be improved to note interaction with
formatter, or the code might be improved to handle formatter interactions.

As always, thanks to Hadley.

Reply all
Reply to author
Forward
0 new messages