1

I have got a data frame like this:

   Days      Ahm5
01/06/1961  0.00000
02/06/1961  0.19266
03/06/1961  1.67610
........   ........
30/09/1961  5.26514
01/06/1962  0.05200
.........   ........
30/09/2007  0.866473

Here's the data:https://www.dropbox.com/s/de88gqk7kvb1q9i/data.csv?dl=0 When I try a scatter plot like,

data <- read.csv("data.csv")
data[data==-9.99e+08] = NA                      
data$days <- as.Date(data$days, format="%d/%m/%Y")
plot(Ahm5~days,data, col='blueviolet', type="p", pch=1 ,cex=.5, xlab="Year",xaxt='n',  ylab="Rainfall(mm/Day)")
axis.Date(side=1,seq(as.Date("1961/1/1"), by = "year", length.out = 47))

output is like this. enter image description here

But my desired output should be like figure given below with x axis span from 1960 to 2007 as in the data:

enter image description here Also how to add last year on x axis? Excuse me for this trivial question.

ajilesh
  • 299
  • 3
  • 13
  • Please include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This makes it easier for others to help you. – Jaap Sep 15 '17 at 09:58
  • Edited with data and code I used. – ajilesh Sep 15 '17 at 10:20

1 Answers1

2

You have to make sure the plot is wide enough, else some of the breaks will be omitted. See the examples below to see the difference.

Example 1:

png(filename = 'scatterplot1.png', width = 800, height = 400)
plot(Ahm5 ~ days, dat, col = 'blueviolet', type = "p", pch = 1, cex=.5, xlab = "Year", xaxt='n',  ylab = "Rainfall (mm/Day)")
axis.Date(side = 1, at = seq(as.Date("1961/1/1"), by = "year", length.out = 47))
dev.off()

gives:

enter image description here

Example 2:

png(filename = 'scatterplot2.png', width = 600, height = 300)
plot(Ahm5 ~ days, dat, col = 'blueviolet', type = "p", pch = 1, cex=.5, xlab = "Year", xaxt='n',  ylab = "Rainfall (mm/Day)")
axis.Date(side = 1, at = seq(as.Date("1961/1/1"), by = "year", length.out = 47))
dev.off()

gives:

enter image description here

Example 3:

png(filename = 'scatterplot3.png', width = 400, height = 200)
plot(Ahm5 ~ days, dat, col = 'blueviolet', type = "p", pch = 1, cex=.5, xlab = "Year", xaxt='n',  ylab = "Rainfall (mm/Day)")
axis.Date(side = 1, at = seq(as.Date("1961/1/1"), by = "year", length.out = 47))
dev.off()

gives:

enter image description here

As you can see the breaks differ between the plots and R chooses which breaks to use. To have nicer breaks, you can use:

png(filename = 'scatterplot4.png', width = 800, height = 400)
plot(Ahm5 ~ days, dat, col = 'blueviolet', type = "p", pch = 1, cex=.5, xlab = "Year", xaxt='n',  ylab = "Rainfall (mm/Day)")
axis.Date(side = 1, at = seq(as.Date("1965/8/1"), by = "5 year", length.out = 9))
dev.off()

which gives:

enter image description here

Now you have:

  • Nicer breaks
  • The ticks are aligned in the middle of the yearly 'scatter-piles'.

UPDATE: A further elaboration on the last example:

png(filename = 'scatterplot5.png', width = 800, height = 400)
plot(Ahm5 ~ days, dat, col = 'blueviolet', type = "p", pch = 1, cex=.5,
     xlab = "Year", xaxt='n',  ylab = "Rainfall (mm/Day)")
axis.Date(side = 1,
          at = c(seq(as.Date("1960/8/1"), by = "10 year", length.out = 5),
                 as.Date('2007-08-01')))
dev.off()

which gives:

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193