增加R图的x轴上标记的刻度线的密度

时间:2017-05-27 15:04:37

标签: r plot

我希望有一个标签对应于大多数事件发生在R plot()中此图的x轴上的年份:

It corresponds to stock market drops

其中一个主要集群在2008年左右。但是,该软件决定标记2010年的刻度线。

有帖子解释如何选择一些给定的标签;然而,如果我在绘图之前不知道这些年,我怎样才能增加xaxis上标记的刻度线的密度以获得大约一年的尖峰? [澄清:我不想查询数据以发现群集是在2008年 - 我只是想增加标记的刻度线的数量,以使其中一个接近尖峰。] < / p>

以下是准备复制和粘贴的代码:

require(RCurl)
require(foreign)
x <- getURL("https://raw.githubusercontent.com/RInterested/datasets/gh-pages/%5EDJI.csv")
DJI <- read.csv(text = x, sep =",")

DJI$Date <- as.Date(DJI$Date, format = "%m/%d/%Y") # Formatting Date as.Date
rownames(DJI) <- DJI$Date          # Assigning Date to row names
DJI.raw <- DJI
DJI$Date <- NULL                   # Removing the Date column
chartSeries(DJI, type="auto", theme=chartTheme('white'))



# Function to calculate % change in closing price between days:
D2D = function (x) {              
  days = nrow(x)
  delta = numeric(days)
  for(i in 2:days){
    delta[i] <- (100*((x[i,1] - x[i - 1,1])/(x[i - 1,1])))
  }
  delta
}

z <- as.data.frame(DJI$Adj.Close)    # Subsetting closing price
DJI$InterDay <- D2D(z)               # Included as add'l column to VTI.
DJI.raw$InterDay <- DJI$InterDay

plot(DJI.raw$Date, DJI.raw$InterDay < -4, pch=19, col=2, type='h', 
     xlab="Year", ylab="Days with > 4% change", 
     cex.axis=.7, cex.main=.8, cex.lab =.8,las=2, 
     main  = "Clustering of big drop days")

后续问题:

如果不是如上所述格式化数据,我尝试将其合并为xts对象,如下所示:

require(RCurl)
require(foreign)
x <- getURL("https://raw.githubusercontent.com/RInterested/datasets/gh-pages/%5EDJI.csv")
DJI <- read.csv(text = x, sep =",")
DJI$Date <- as.Date(DJI$Date, format = "%m/%d/%Y") # Formatting Date as.Date
rownames(DJI) <- DJI$Date          # Assigning Date to row names
DJI$Date <- NULL                   # Removing the Date column
DJI <- as.xts(DJI)
chartSeries(DJI, type="auto", theme=chartTheme('white'))

time(DJI)[DJI$Close == min(DJI$Close)]


# Function to calculate % change in closing price between days:
D2D = function (x) {              
  days = nrow(x)
  delta = numeric(days)
  for(i in 2:days){
    delta[i] <- (100*((x[i,1] - x[i - 1,1])/(x[i - 1,1])))
  }
  delta
}

z <- as.data.frame(DJI$Adj.Close)    # Subsetting closing price
DJI$InterDay <- D2D(z)               # Included as add'l column to VTI.
DJI.raw$InterDay <- DJI$InterDay


plot(time(DJI), DJI$InterDay < -4, col=2, type='h', 
     xlab="Year", ylab="Days with > 4% change",
     cex.axis=.7, cex.main=.8, cex.lab =.8,las=2, 
     main  = "Clustering of big drop days")

我怎样才能获得更具信息量的x轴?

2 个答案:

答案 0 :(得分:1)

您可以使用plot(..., xaxt="n")以便不打印x-ax,然后使用axis(1, at=2000:2016)添加所有年份标签。
顺便说一句,我不明白为什么你说你不知道绘图前几年因为这是你的数据!您始终可以使用cuttable来计算某个范围内的数据数量...您还可以简单地将x数据的最小值和最大值四舍五入作为{的最小值和最大值。 {1}}
有了日期,你应该使用类似的东西:

axis

如果您想在数据的位置添加刻度数年,您可以试试这个。这不是很干净,因为你可以多次使用多年,但这是一个开始:

plot(DJI.raw$Date, DJI.raw$InterDay < -4, pch=19, col=2, type='h', 
     xlab="Year", ylab="Days with > 4% change", xaxt="n",
     cex.axis=.7, cex.main=.8, cex.lab =.8,las=2, 
     main  = "Clustering of big drop days")

axis(1, at=as.Date(paste0(1985:2016, "-01-01")), labels = 1985:2016)

答案 1 :(得分:0)

显然,OP有一个固有的问题,因为它最初将extensive time series (xts)作为带有is.data.frame(DJI) [1] TRUE格式的行名称的临时as.Date()来处理,允许{{{}等函数强制执行1}},否则会产生chartSeries;同时,将"chartSeries requires an xtsible object"时间列保留在用于子设置的并行数据框(Date)内。

不幸的是,低效编码根源的这些概念性问题在页面的其余部分被刷过,并且&#34;操作方法&#34;食谱优先于真正的教学指导。此外,帖子应该被标记为重复,因为有一个完美解决问题的有价值的答案here。由于此时我无法删除OP,因此我会将漂亮answer by @A5C1D2H2I1M1N2O1R2T1的应用发布到当前问题。

使用适当的格式来处理时间索引数据,这是一个避免OP中不必要的重复数据帧的解决方案:

将数据集导入并格式化为DJI.raw

xts

附加列,其功能是将结束日期之间的差异生成为require(RCurl) require(foreign) x <- getURL("https://raw.githubusercontent.com/RInterested/datasets/gh-pages/%5EDJI.csv") DJI <- read.csv(text = x, sep =",") DJI$Date <- as.Date(DJI$Date, format = "%m/%d/%Y") # Formatting Date as.Date rownames(DJI) <- DJI$Date # Assigning Date to row names DJI$Date <- NULL # Removing the Date column DJI <- as.xts(DJI) 的附加列:

DJI

最后是情节......首先没有x轴:

# Function to calculate % change in closing price between days:
D2D = function (x) {              
  days = nrow(x)
  delta = numeric(days)
  for(i in 2:days){
    delta[i] <- (100*((x[i,1] - x[i - 1,1])/(x[i - 1,1])))
  }
  delta
}

z <- as.data.frame(DJI$Adj.Close)    # Subsetting closing price
DJI$InterDay <- D2D(z)               # Included as add'l column to VTI.
#... we need something to fill in the 0 in row 1. Why not the second value?
DJI$InterDay[1]<-DJI$InterDay[2]

enter image description here

...并添加x轴:

plot(time(DJI), DJI$InterDay < -4, col=2, type='h', xaxt="n",
     xlab="Year", ylab="Days with > -4% change",
     cex.axis=.7, cex.main=.8, cex.lab =.8,las=2, 
     main  = "Clustering of big drop days")

enter image description here