更改R中Y轴上的中断(时间序列)

时间:2019-03-21 15:31:38

标签: r time-series

我有一个数据集,显示了19年内各种城市的房价,这是它的样子。

Dataset

我正在尝试为此数据创建时间序列图,到目前为止,这是我的代码

    Prices <- read.csv("Second Hand Appartment Prices.csv", header=TRUE, skip = 1)
colnames(Prices)[colnames(Prices)=="X"] <- "Year"



National <- ts(Prices$National, start = 1997)
Dublin <- ts(Prices$Dublin, start = 1997)
Cork <- ts(Prices$Cork, start = 1997)
Galway <- ts(Prices$Galway, start = 1997)
Limerick <- ts(Prices$Limerick, start = 1997)
Waterford <- ts(Prices$Waterford, start = 1997)
OtherAreas <- ts(Prices$Other.Areas, start = 1997)
color <- rainbow(ncol(Prices))
ts1 <- ts.plot(National, Dublin, Cork, Galway, Limerick, Waterford, OtherAreas, xlab = "Years", ylab ="Average Price", main = "Second Hand Appartment Prices 1997-2005", col = color)
legend("bottom", legend = colnames(Prices), lty=1, col = color,cex=0.5)

我正在尝试更改时间序列图,以使休息时间例如为0,50000,100000,等等

有人可以帮我吗?非常感谢

我只能使用作业R中指定的基本R图形

这是我的代码当前正在生成的

Time series plot

1 个答案:

答案 0 :(得分:1)

您可以尝试

ts.plot(National, Dublin, Cork, Galway, Limerick, Waterford, OtherAreas, 
        gpars = list(yaxt = 'n'))
axis(side = 2, 
     at = seq(from = 0, to = max(Prices[, colnames(Prices) != 'Year']), by = 50000), 
     labels = seq(from = 0, to = max(Prices), by = 50000))

自变量yaxt='n'抑制y轴上的刻度。然后,函数axis()然后绘制所需的刻度。您可以在这里找到更多详细信息:https://www.statmethods.net/advgraphs/axes.html

同时,您可能需要仔细检查ts对象的值:您的图看起来很有趣,因为y轴刻度线绘制的值与数据集快照的值非常不同。

顺便说一下,Year不是时间序列,因此您可以将其从图例中删除。