Now, I think I am making this harder than it seems, but... I am writing a script that will allow me to pull any csv file with annual rainfall and plot it into a bar plot. The problem I am facing is that the different sites have recorded data for a different number of years. This means that every csv file has a different number of values. I need to somehow get a label at every 10 or 20 year points, and it has be able to allow for the fact that some files have 50 readings, and others have 200 readings. Now, if I use this: #axis(1,at=seq(1,rows,10),tcl=-0.5,labels=c(kent[1,2],kent[11,2],kent[21,2],kent[31,2],kent[41,2],kent[51,2],kent[61,2],kent[71,2],kent[81,2],kent[91,2]... I can get it for one file, but as soon as a file has a diff number of values, it does not work. The best I have been able to come up with is this: barplot(Ann,main=title, xlab="Year",ylab="Rainfall (mm)",ylim=c(0,ymax),col="blue",space=0,names.arg=c(kent[,2]),cex.names=0.8) however I cannot control how often the labels occur. I am sure there is an easy way to do this, but I cannot find it. I assume it will have something to do with names.arg, but I am not sure how to write what i want to write. Cheers, in advance. -- View this message in context: http://n4.nabble.com/Barplot-axis-titles-tp1476789p1476789.html Sent from the R help mailing list archive at Nabble.com.
On 02/11/2010 01:03 PM, RagingJim wrote:> > Now, I think I am making this harder than it seems, but... > > I am writing a script that will allow me to pull any csv file with annual > rainfall and plot it into a bar plot. The problem I am facing is that the > different sites have recorded data for a different number of years. This > means that every csv file has a different number of values. > > I need to somehow get a label at every 10 or 20 year points, and it has be > able to allow for the fact that some files have 50 readings, and others have > 200 readings. > > Now, if I use this: > > #axis(1,at=seq(1,rows,10),tcl=-0.5,labels=c(kent[1,2],kent[11,2],kent[21,2],kent[31,2],kent[41,2],kent[51,2],kent[61,2],kent[71,2],kent[81,2],kent[91,2]... > > I can get it for one file, but as soon as a file has a diff number of > values, it does not work. > > The best I have been able to come up with is this: > > barplot(Ann,main=title, xlab="Year",ylab="Rainfall > (mm)",ylim=c(0,ymax),col="blue",space=0,names.arg=c(kent[,2]),cex.names=0.8) > > however I cannot control how often the labels occur. I am sure there is an > easy way to do this, but I cannot find it. I assume it will have something > to do with names.arg, but I am not sure how to write what i want to write.Hi RagingJim, How about this? dimkent<-dim(kent) axis.ticks<-seq(kent[1,2],kent[dimkent[1],2],by=20) if(kent[dimkent[1],2]%%20) axis.ticks<-axis.ticks[-length(axis.ticks] barplot(Ann,main=title, xlab="Year",ylab="Rainfall (mm)", ylim=c(0,ymax),col="blue",space=0, names.arg=axis.ticks,cex.names=0.8) Jim
Cheers Jim. However when I input: axis.ticks<-seq(kent[1,2],kent[dimkent[34,2]],by=10) I get the error "Error in dimkent[1,2] : incorrect number of dimensions" What are these dimensions that I am meant to have in there? I have tried total dimensions of the table, dimension of the row that I need, but I still continue to get the same error. Everything I type after here is more an explanation of what I have tried, and superfluous to the actual current problem. If I use the data.frame of "kent", the dimensions are 34,16, and is a matrix. For some reason whenever I try to create a new matrix/data frame, the "Year" column becomes 1,2,3,4,5,6...34 etc. This is because the original data.frame has the year column as a fact: $ Year : Factor w/ 39 levels "1977","1978",..: 1 2 3 4 5 6 7 8 9 10 ... and as soon as I attempt to withdraw the years, it turns them into 1,2,3 etc. If I make a table i.e kent=as.table(kent) and cut it down to 2 rows, it seems to have number values that I can remove. This new table (which as I mentioned is the only I seem to be able to have a table of the two relevant columns (year and rainfall), has new dimensions of 34,2. Thanks again mate. -- View this message in context: http://n4.nabble.com/Barplot-axis-titles-tp1476789p1477933.html Sent from the R help mailing list archive at Nabble.com.
ok, used the lapply function and now the "Years" column is the numeric type. However, I still get the same error as above. Replacing either "kent[1,1]" or "kent[dimkent[1],1]" with a number, gives me the same error. If both terms are replaced with a number, then it does not produce an error. It then runs, and the number "1" is the only number on the bottom axis. However after the next line: if(kent[dimkent[1],2]%%20) axis.ticks<-axis.ticks[-length(axis.ticks)] I get this error: "Error in kent[dimkent[1], 2]%%20 : non-numeric argument to binary operator" -- View this message in context: http://n4.nabble.com/Barplot-axis-titles-tp1476789p1477994.html Sent from the R help mailing list archive at Nabble.com.
To everyone else, it is going to look like I am talking to myself. Jim, I got your reply (to my post which I changed almost as soon as I posted it), and "as.numeric(as.character(kent$Year))" is a god send. Worked a charm, and fixed all the problems. So thanks :) Last issue now (at least for this bit), is that when I use "names.arg=axis.ticks" it comes up with this error: "Error in barplot.default(Ann, main = title, names.arg = axis.ticks) : incorrect number of names" Does this mean it is trying to fit too many names onto the barplot? and if so, how do I reduce it or fix it? Thanks again mate, really appreciate it! -- View this message in context: http://n4.nabble.com/Barplot-axis-titles-tp1476789p1478006.html Sent from the R help mailing list archive at Nabble.com.