tick labels missing with scale_x_discrete()

4,926 views
Skip to first unread message

Wind

unread,
Nov 4, 2010, 1:21:53 AM11/4/10
to ggp...@googlegroups.com
Hi,

I could not make the tick labels displayed with the scale_x_discrete(breaks=..., labels=...).
Codes as following:

> library(ggplot2)
> dd <- data.frame(xx=paste("2010-",1:10,sep=""),yy=runif(10,0,5))
> dd
        xx        yy
1   2010-1 2.7251934
2   2010-2 3.1082813
3   2010-3 3.8652348
4   2010-4 2.1694184
5   2010-5 0.5826324
6   2010-6 2.6099141
7   2010-7 2.7677792
8   2010-8 3.1773559
9   2010-9 0.1525337
10 2010-10 2.4040786
> (c <- ggplot(dd,aes(xx,yy))+geom_bar(stat="identity"))
.........chart displayed ok.

c+scale_x_discrete(breaks=c(1,5,10),labels=c("a","b","c"))
......... there are correct breaks but no tick labels on the chart 


sessionInfo()
> > > R version 2.11.1 (2010-05-31) 
i386-pc-mingw32 

locale:
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936 
[2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936   
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936
[4] LC_NUMERIC=C                                                   
[5] LC_TIME=English_United States.1252                             

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] ggplot2_0.8.8 proto_0.3-8   reshape_0.8.3 plyr_1.2.1   

It seems that if x axis is continuous then everything would be OK.

Any suggestion would be appreciated.
Thanks in advance.


Wind

Brandon Hurr

unread,
Nov 4, 2010, 7:28:58 AM11/4/10
to Wind, ggp...@googlegroups.com
This seems to work. 

ggplot(dd,aes(xx,yy))+geom_bar(stat="identity")+scale_x_discrete(breaks=c("2010-1","2010-5","2010-10"),labels=c("a","b","c"))

You'll need to reorder your xx though since 2010-10 comes after 2010-1 and not 2010-9.

Brandon

Wind

unread,
Nov 4, 2010, 5:41:13 PM11/4/10
to Brandon Hurr, ggp...@googlegroups.com
Thanks Brandon,

Your codes work exactly as what I need.

Regards,
Wind
Reply all
Reply to author
Forward
0 new messages