Ragy Isaac
Ragy Isaac

Reputation: 1458

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.

Upvotes: 2

Views: 7734

Answers (3)

Henrik
Henrik

Reputation: 67778

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.

Upvotes: 2

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

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)

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)

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)

enter image description here

Upvotes: 1

lukeA
lukeA

Reputation: 54237

at = seq(1,12) does not work because dates are stored as numerics and 1 to 12 don't match the dates internal representation. See e.g. axis(side = 1, at = Labels) and the help ?Dates:

Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates.

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

works.

However, if you want the labels to be slanted in base graphics, I think you need to use text() with srt:

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

Finally, use format() with big.mark= to format the y-labels:

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

Upvotes: 1

Related Questions