Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

r - Axis does not plot with date labels

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)

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)

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)

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

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.

Many thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may try a ggplot alternative:

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

If you want to set point shapes and colours manually, see ?scale_shape_manual and ?scale_color_manual.

Edit: Changed date format on x axis.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...