如何标记多年的时间与人口情节?

时间:2015-06-17 22:56:54

标签: r plot time-series

我想绘制年份与人口的关系,并在下面给出一个非常简单的R图:

enter image description here

以下是情节的R代码:

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))

我希望在小圈子附近出现几年。那可能吗?我使用identify(year,population),但只是数字圈为1,2,3,...,这不是我想要的。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

您可以使用$2

text

enter image description here

如果你想尝试让年份偏移一点,这是一个开始,

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

偏移量只是1或-1的向量,由先前的总体小于或大于当前的数量确定。