轴不绘制日期标签 [英] Axis does not plot with date labels

查看:69
本文介绍了轴不绘制日期标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由以下数据代表的多元数据集:

I have a multivariate data set typified by the data below:

financials <- 
  "A           B         C          D            E         Dates
52730.1     104761.1    275296.1     569423.1  1136638.1    2013-12-2
77709       97940       275778      515095     1075166      2013-08-04
102734      71672       227017      482068     1011764      2013-03-17
96345       74035       240334      429026      998734      2012-10-28 
98651       62305       236694      436948      962913      2012-06-10 
78804       73568       242068      471640      945891      2012-01-22"
fData <- read.table(text = financials, header = TRUE)

我在同一张图上绘制了所有变量(A、B 等).这是我的代码,它有效:

I plot all variables (A, B,.. etc) on the same plot. Here is my code and it works:

 range <- range(fData[,-6])
fData$Dates <- as.Date(fData$Dates, origin = "2011-07-03")
Labels <- seq(as.Date("2012-01-22", origin = "2011-07-03"),
              to = as.Date("2013-12-2",origin = "2011-07-03"),
              by = "2 months")
plot(A ~ Dates, fData,  type = "o", col = "red", ylim = range, xaxt="n", pch = 10)
points(B ~ Dates, fData, type = "o", col = "green", pch = 11)
points(C ~ Dates, fData, type = "o", col = "black", pch = 12)
points(D ~ Dates, fData, type = "o", col = "blue", pch = 13)
points(E ~ Dates, fData, type = "o", col = "magenta", pch = 14)

我尝试了函数axis来添加x轴,如下图,但是x轴没有出现

I tried the function axis to add the x-axis, as below, but the x-axis did not show-up

axis(side = 1, at = seq(1,12), labels = Labels)

然后我尝试了axis.Date函数我得到一个错误'origin' must be provided.

Then I tried axis.Date function I got an error 'origin' must be supplied.

axis.Date(side = 1, x = Labels, at = seq(1,12), format = "%m/%y", origin = "2011-07-03") 

我需要的是:(1) 12 个日期标签标签",在 x 轴上有 12 个刻度线,倾斜 45 度,(2) 格式化 y 轴以显示以 100,000 美元为单位的财务数据,(3)请帮助我理解我的错误.

What I need is: (1)12 date labels "Labels" with 12 tick marks on the x-axis SLANTED at 45 degrees and (2) format the y-axis to display financial data in $100,000 ticks, (3) please help me understand my mistakes.

非常感谢.

推荐答案

首先,创建没有轴的绘图 (axes = FALSE):

First, create the plot without axes (axes = FALSE):

plot(A ~ Dates, fData,  type = "o", col = "red", ylim = range, pch = 10,
     axes = FALSE)
points(B ~ Dates, fData, type = "o", col = "green", pch = 11)
points(C ~ Dates, fData, type = "o", col = "black", pch = 12)
points(D ~ Dates, fData, type = "o", col = "blue", pch = 13)
points(E ~ Dates, fData, type = "o", col = "magenta", pch = 14)

您不能使用 axis.Date 作为 x 轴,因为您需要倾斜的标签.您必须将 axis(用于刻度线)和 text 结合起来用于标签:这里,format 用于创建标签:

You can't use axis.Date for the x-axis since you want slanted labels. You have to combine axis (for the tick marks) and text for the labels: Here, format is used the create the labels:

axis(side = 1, at = Labels, labels = FALSE) 
text(x = Labels, y = par("usr")[3] - 70000, labels = format(Labels, "%m/%y"), 
     srt = 45, pos = 1, xpd = TRUE)

现在,您必须创建 y 轴.首先,我们需要一个刻度位置向量 (ticksY) 和一个标签向量 (LabelsY).值采用指定格式:

Now, you have to create the y-axis. First, we need a vector of tick positions (ticksY) and a vector of labels (LabelsY). The values are in the specified format:

ticksY <- seq(0, max(range), 100000)
LabelsY <- paste("$", format(ticksY, scientific = FALSE, big.mark = ","))
axis(side = 2, at = ticksY, labels = LabelsY)

这篇关于轴不绘制日期标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆