2

I want to plot years versus population and have a very simple R plot given below:

enter image description here

Here is the R code for the plot:

dat <- data.frame(population, year)
year <- c(1518,1523,1569,1642,1871,1872,1873,1874,1875,1876,1877,1894,1897,1898,1899,1900,  1914,1927)
population <- c(753,2407,4414,1800,7896,6594,6594,6594,6594,6594,6615,8583,8583,8583,7470,7540,12266,10573)
 offsets <- c(1, with(dat, ifelse(dat$population[-1] < dat$population[-     nrow(dat)], -1, 1)))
 plot(population ~ year, data=dat, type="l")
 points(dat$year, dat$population)
 text(dat$year-10, dat$population+500*offsets, labels=round(dat$year,0))

I want years to appear near the small circles. Is that possible? I used identify(year,population) but that just numbers circles as 1,2,3,..., which is not what I want. Any help?

Günal
  • 751
  • 1
  • 14
  • 29

1 Answers1

1

You could use text

dat <- data.frame(pop=seq.int(2000,12000,len=20)+rnorm(20,0,2000), 
                  year=seq.int(1500,1900,len=20))

plot(pop ~ year, data=dat, type="l")
text(dat$year, dat$pop, labels=round(dat$year,0))

enter image description here

If you want to try and make the years offset a bit, here is a start,

offsets <- c(1, with(dat, ifelse(pop[-1] < pop[-nrow(dat)], -1, 1)))
plot(pop ~ year, data=dat, type="l")
points(dat$year, dat$pop)
text(dat$year-10, dat$pop+500*offsets, labels=round(dat$year,0))

enter image description here

The offsets is just a vector of 1 or -1, determined by the previous population being smaller or larger than the current.

Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • That works, thanks. However, in my data set, some years are very close to each other. Therefore, year names are written very close to each other, making it difficult to read years. Is there a solution for that? I added the plot I get above – Günal Jun 17 '15 at 23:14
  • I have added my code for real data above. It does not look a lovely as yours. Any idea why? – Günal Jun 17 '15 at 23:30
  • well, you have a bunch of measurements right on top of eachothers. Do you jus want to report one year in that case? – Rorschach Jun 17 '15 at 23:33
  • It would be lovely to report each year. Would it make sense to use arrows which I don't know how or another plot? – Günal Jun 17 '15 at 23:34
  • if you want arrows to label clustered points, the way I imagine doing it would be a fair amout of work, you might want to start a new question on that topic. The easiest way might be to just program each arrow and label by itself so you have full control over where it points and the label appears. – Rorschach Jun 17 '15 at 23:41
  • This issue has already been discussed at a highly-voted question from a while ago - [intelligent point label placement in r](http://stackoverflow.com/questions/7611169/intelligent-point-label-placement-in-r) – thelatemail Jun 17 '15 at 23:43
  • @thelatemail I don't see anything about arrows in that post – Rorschach Jun 17 '15 at 23:47
  • @thelatemail, me either – Günal Jun 17 '15 at 23:54
  • @Günal - I wasn't referring to arrows. Intelligently spacing labels is however covered by that topic. – thelatemail Jun 18 '15 at 00:02