Axis不会使用日期标签绘图

时间:2014-01-26 19:19:48

标签: r plot

我有一个多变量数据集,以下面的数据为代表:

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,..等)。这是我的代码,它有效:

 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)

我尝试使用功能轴添加x轴,如下所示,但是x轴没有显示

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

然后我尝试了axis.Date函数我得到了一个错误'origin'必须提供。

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)请帮我理解我的错误。

非常感谢提前。

3 个答案:

答案 0 :(得分:2)

您可以尝试ggplot替代方案:

library(reshape2)
library(ggplot2)
library(scales)

df <- melt(fData)
df$Dates <- as.Date(df$Dates)

ggplot(data = df, aes(x = Dates, y = value, color = variable)) +
  geom_point() +
  geom_line() +
scale_x_date(breaks = date_breaks("2 month"),
             labels = date_format("%Y-%m")) +
  scale_y_continuous(labels = dollar,
                     breaks = seq(from = 100000, to = 1200000, by = 100000)) +
  theme_classic() + 
  theme(axis.text.x  = element_text(angle = 45, vjust = 1, hjust = 1))

enter image description here

如果您想手动设置点形状和颜色,请参阅?scale_shape_manual?scale_color_manual

修改:更改了x轴上的日期格式。

答案 1 :(得分:1)

at = seq(1,12)不起作用,因为日期存储为数字,1到12与日期内部表示不匹配。参见例如axis(side = 1, at = Labels)和帮助?Dates

  

日期表示为自1970-01-01以来的天数   早期日期的负值。

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

的工作原理。

但是,如果您希望标签在基础图形中倾斜,我认为您需要将text()srt一起使用:

text(Labels, par("usr")[3], srt = 45, adj = 1, labels = Labels, xpd = TRUE)

最后,使用format()big.mark=格式化y标签:

axis(side = 2, at = pretty(as.matrix(fData[, -6])), labels = format(pretty(as.matrix(fData[, -6])), big.mark=","), las=1)

答案 2 :(得分:1)

首先,创建没有轴(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用于创建标签:

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)。值采用指定的格式:

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

enter image description here