[R] reducing distances between tickmarks

David Winsemius dwinsemius at comcast.net
Mon Oct 4 21:56:35 CEST 2010


On Oct 4, 2010, at 3:28 PM, Dimitri Liakhovitski wrote:

> Hello, everybody!
> I have a code below that creates a data set and then a stacked bar
> chart based on that data set.
> No need to look at it - just notice please that my horizontal axis is
> a date varible (x=my.data$date).
> I have a question about the last 2 lines of this code:
> grid(nx=NULL,ny=NULL,col = "lightgray", lty = "dotted",lwd =  
> par("lwd"))
> axis(1, las = 2)

But you only gave two arguments to axis... the "side" and "las"???
>
> Could anyone tell me - what are the tick mark labels on my X axis? Why
> are they not dates?

Because they are the number of days since the "origin". Read the help  
page for Dates. If you want the text value for axis to be formatted,  
then consult the axis help page for the argument names and use the  
appropriate call to format:

?Dates
?axis  #   perhaps ... labels =format(my.data$date, "%Y-%m-%d")

# but you will also need to decide where you want the tick marks

Perhaps:

 > axis(1, labels =format(as.Date(axTicks(1), origin="1970-01-01"),  
"%Y-%m-%d"), at=axTicks(1), col="red")



?format


> axTicks(side=1)
>
> Is there any way to change them to actual dates as in my.data$date?
> And also - is there a way to force the graph to display all tickmarks
> on X axis - and not just 4 of them?
>
> Thank you!
> Dimitri
>
> my.data<- 
> data.frame(date=c(20080301,20080401,20080501,20080601,20080701,20080801,20080901,20081001,20081101,20081201,20090101,20090201,20090301,20090401,20090501,20090601,20090701,20090801,20090901,20091001,20091101,20091201,20100101,20100201,20100301,20100402,20100503),
> x=c(1.1, 1, 1.6, 1, 2, 1.5, 2.1, 1.3, 1.9, 1.1, 1, 1.6, 1, 2, 1.5,
> 2.1, 1.3, 1.9, 1.1, 1, 1.6, 1, 2, 1.5, 2.1, 1.3, 1.9),
> y 
> = 
> c 
> (-4 
> ,-3 
> ,-6 
> ,-5 
> ,-7 
> ,-5.2 
> ,-6 
> ,-4 
> ,-4.9,-4,-3,-6,-5,-7,-5.2,-6,-4,-4.9,-4,-3,-6,-5,-7,-5.2,-6,-4,-4.9),
> z 
> = 
> c 
> (-0.2 
> ,-0.3 
> ,-0.4 
> ,-0.1 
> ,-0.2 
> ,-0.05 
> ,-0.2 
> ,-0.15 
> ,-0.06 
> ,-0.2 
> ,-0.3 
> ,-0.4 
> ,-0.1 
> ,-0.2 
> ,-0.05 
> ,-0.2,-0.15,-0.06,-0.06,-0.2,-0.3,-0.4,-0.1,-0.2,-0.05,-0.2,-0.15),
> a 
> = 
> c 
> (10,13,15,15,16,17,15,16,14,10,13,15,15,16,17,15,16,14,10,13,15,15,16,17,15,16,14 
> ))
> my.data$date<-as.character(my.data$date)
> my.data$date<-as.Date(my.data$date,"%Y%m%d")
> (my.data)
> str(my.data)
>
> positives<-which(colSums(my.data[2:ncol(my.data)])>0) # which vars
> have positive column sums?
> negatives<-which(colSums(my.data[2:ncol(my.data)])<0) # which vars
> have negative column sums?
>
> y.max<-1.1*max(rowSums(my.data[names(positives)])) # the max on the y
> axis of the chart
> y.min<-1.1*min(rowSums(my.data[names(negatives)])) # the min on the y
> axis of the chart
> ylim <- c(y.min, y.max)
> order.positives<-rev(rank(positives))
> order.of.pos.vars<-names(order.positives)
> order.negatives<-rev(rank(negatives))
> order.of.neg.vars<-names(order.negatives)
> order<-c(order.negatives,order.positives)
> order.of.vars<-names(order)   # the order of variables on the chart -
> from the bottom up
> ### so, the bottom-most area should be for z, the second from the
> bottom area- for y (above z)
>
> all.colors<-c('red','blue','green','orange','yellow','purple')
> xx <- c(my.data$date, rev(my.data$date))
> bottom.y.coordinates<-rowSums(my.data[names(negatives)])
>
> plot(x=my.data$date, y=bottom.y.coordinates, ylim=ylim, col='white',
> type='l', xaxt='n',
> 	ylab='Title for Y', xlab='Date', main='Chart Title')
>
> for(var in order.of.neg.vars){
> 	top.line.coords<-bottom.y.coordinates-my.data[[var]]
> 	bottom.coords<-c(bottom.y.coordinates,rev(top.line.coords))
> 	polygon(xx,bottom.coords,col=all.colors[which(names(my.data) %in%  
> var)])
> 	bottom.y.coordinates<-top.line.coords
> }
>
> for(var in order.of.pos.vars){
> 	top.line.coords<-bottom.y.coordinates+my.data[[var]]
> 	bottom.coords<-c(bottom.y.coordinates,rev(top.line.coords))
> 	polygon(xx,bottom.coords,col=all.colors[which(names(my.data) %in%  
> var)])
> 	bottom.y.coordinates<-top.line.coords
> }
>
> grid(nx=NULL,ny=NULL,col = "lightgray", lty = "dotted",lwd =  
> par("lwd"))
> axis(1, las = 2)
>
>
> -- 
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list