plot time series with discontinuous axis in R - r

I want to plot a time series, excluding in the plot a stretch of time in the middle. If I plot the series alone, with only an index on the x-axis, that is what I get. The interior set of excluded points do not appear.
x <- rnorm(50)
Dates <- seq(as.Date("2008-1-1"), by = "day", length.out = length(x))
dummy <- c(rep(1, 25), rep(0, 10), rep(1, length(x) - 35))
plot(x[dummy == 1])
Once the dates are on the x-axis, however, R dutifully presents an accurate true time scale, including the excluded dates. This produces a blank region on the plot.
plot(Dates[dummy == 1], x[dummy == 1])
How can I get dates on the x-axis, but not show the blank region of the excluded dates?

Three alternatives:
1. ggplot2 Apparently, ggplot2 would not allow for a discontinuous axis but you could use facet_wrap to get a similar effect.
# get the data
x = rnorm(50)
df <- data.frame( x = x,
Dates = seq(as.Date("2008-1-1"), by = "day", length.out = length(x)) ,
dummy = c(rep(1, 25), rep(0, 10), rep(1, length(x) - 35)))
df$f <- ifelse(df$Dates <= "2008-01-25", c("A"), c("B"))
# plot
ggplot( subset(df, dummy==1)) +
geom_point(aes(x= Dates, y=x)) +
facet_wrap(~f , scales = "free_x")
2. base R
plot(df$x ~ df$Dates, col= ifelse( df$f=="A", "blue", "red"), data=subset(df, dummy==1))
3. plotrix Another alternative would be to use gap.plot{plotrix}. The code would be something like this one below. However, I couldn't figure out how to make a break in an axis with date values. Perhaps this would and additional question.
library(plotrix)
gap.plot(Dates[dummy == 1], x[dummy == 1], gap=c(24,35), gap.axis="x")

I think I figured it out. Along the lines I proposed above, I had to fiddle with the axis command for a long time to get it to put the date labels in the right place. Here's what I used:
plot(x[dummy == 1], xaxt = "n", xlab = "") # plot with no x-axis title or tick labels
Dates1_index <- seq(1,length(Dates1), by = 5) # set the tick positions
axis(1, at = Dates1_index, labels = format(Dates1[Dates1_index], "%b %d"), las = 2)
Having succeeded, I now agree with #alistaire that it looks pretty misleading. Maybe if I put a vertical dashed line at the break...

Related

format of the x-axis from 1 e+06 to 1 Mio

i want to change the format of the x-axis of a plot in R from 1 e+06 to 1 Mio.
What is the right comand to do that?
Thank you for your help.
Jonas
Assuming you work with a base R scatter plot and further assuming you mean what you say, namely that you want labels like "1 Mio" (instead of the number 1000000) on the x-axis, this could work for you:
DATA:
x <- seq(1000000, 10000000, 1000000)
y <- 1:10
SOLUTION:
plot(x,y, xaxt = "n")
axis(1, at = x, labels = paste(1:10, "Mio", sep = " "), las = 2)
This solution avoids printing the default x-axis labels by setting xaxt = "n"and by defining a customized x-axis including the desired axis labels

Deviation chart in base graphics

I need to do a deviance chart (lollipop chart with lines from the mean to values above / below the mean). From this question and answer Drawing line segments in R, it is clear that I need to plot segments and then add the points. However, my x axis is a factor and the solution fails.
This works:
df <- data.frame(ID = c(1, 2, 3),
score = c(30, 42, 48))
mid <- mean(df$score)
plot(range(df$ID), range(df$score),type="n")
segments(df$ID, df$score, df$ID, mid)
But changing my identifier variable into a factor breaks it.
df$ID2 <- factor(df$ID)
plot(range(df$ID2), range(df$score),type="n")
segments(df$ID2, df$score, df$ID2, mid)
How can I set up the plot area and x-axis values to deal with a factor?
Note that I need a base graphics solution to fit with the other charts in a dashboard style report.
You can convert the factor in a numeric variable, supress the x-axis and then add the correct labels to the plot:
df$ID2 <- factor(letters[df$ID]) # Use letters to show that this is working
plot(range(as.numeric(df$ID2)), range(df$score), type = "n", xaxt = "n")
segments(as.numeric(df$ID2), df$score, as.numeric(df$ID2), mid)
axis(1, at = seq_along(levels(df$ID2)), labels = levels(df$ID2))

Defining Axis function in R

When drawing axis for time series, we usually create the tick marks and add the axis separately.
Since I have to plot many time series, I tried to write the following function :
Simple plot command
set.seed(1)
x <- as.ts(rnorm(1:150))
plot <- plot(x, xaxt = "n", yaxt = "n")
Convert a set of commands from this answer into a function
tt = seq(as.Date("1994-03-01"), by="months", length=150)
tsAxis <- function (tt) {
ix <- seq_along(tt) # number of ticks
fmt <- "%b-%y" # format of time
labs <- format(tt, fmt) # names or labels of dates
axis(1, at = tt[ix], labels = labs[ix],
tcl = -0.7, cex.axis = 0.7, las = 2)
}
Then tsAxis(tt) must draw the axis but it doesnot and there is no error either.
Even typing the commands separately does not plot the axis.
Any solutions?
It seems that R represents the x-axis with integers in this case (i.e. x=1 is the first event, x=2 is the second event, etc.). You can see this if you run the code:
set.seed(1)
x <- as.ts(rnorm(1:150))
# Take a look at the x-axis
plot <- plot(x)
We can modify your code to reflect this:
tt = seq(as.Date("1994-03-01"), by="months", length=150)
tsAxis <- function (tt) {
ix <- seq_along(tt) # number of ticks
fmt <- "%b-%y" # format of time
labs <- format(tt, fmt) # names or labels of dates
# Change "at=tt[ix]" to "at=ix" here!
axis(1, at = ix, labels = labs[ix],
tcl = -0.7, cex.axis = 0.7, las = 2)
}
Or if you want to plot every third tick mark, just change ix <- seq_along(tt) to ix <- seq(1, length(tt), 3) and it should work.

X axis on a two-screen zoo plot

This time I am struggling to adjust the X axis in a two-screen zoo plot.
Here's an example, inspired from a previous post:
x.Date <- as.Date(paste(rep(2003:2004, each = 12), rep(1:12, 2), 1, sep = "-"))
x <- zoo(data.frame(rnorm(24), rnorm(24)), x.Date)
then when I plot without X axis and add an axis separately
plot(x, xaxt = "n")
axis(1, at = time(x), labels = FALSE)
nothing happens (also with more complex versions of the axis, for example with labels)...
What am I doing wrong?
Many thanks
Benoit
One idea is to use xyplot.zoo the lattice version of plot.zoo. Very easy to customize your x-axis using scales parameter.
library(lattice)
xyplot(x,
scales=list(
x=list( at=time(x),
format='%y-%m')
))

Label X Axis in Time Series Plot using R

I am somewhat new to R and have limited experience with plotting in general. I have been able to work get my data as a time series object in R using zoo, but I am having a hard time having the xaxis be labeled correctly, if it all.
When I plot my zoo object
plot(z)
The x-axis only shows one label, the year 2010. when the series is weekly spanning from April 2009 to October 2010.
I tried to convert my series back to a ts object, and even a data frame (only one column and doesn't include the dates).
Simply, how can I control the x-axis labels generally, and with time series objects?
Thanks in advance!
Start with an example:
x.Date <- as.Date(paste(rep(2003:2004, each = 12), rep(1:12, 2), 1, sep = "-"))
x <- zoo(rnorm(24), x.Date)
plot(x)
If we want different tick locations, we can suppress the default axis plotting and add our own:
plot(x, xaxt = "n")
axis(1, at = time(x), labels = FALSE)
Or combine them:
plot(x)
axis(1, at = time(x), labels = FALSE)
You need to specify the locations for the ticks, so if you wanted monthly, weekly, etc values (instead of observations times above), you will need to create the relevant locations (dates) yourself:
## weekly ticks
plot(x)
times <- time(x)
ticks <- seq(times[1], times[length(times)], by = "weeks")
axis(1, at = ticks, labels = FALSE, tcl = -0.3)
See ?axis.Date for more details, plus ?plot.zoo has plenty of examples of this sort of thing.
The axis labeling doesn't line up with even monthly divsions but may be useful in some situations. Random data (summed) over last 500 days:
xx.Date <- as.Date((Sys.Date()-500):Sys.Date())
x <- zoo(cumsum(rnorm(501)), xx.Date)
tt=time(x)
plot(x, xaxt ="n")
tt <- time(x)
ix <- seq(1, length(tt), by=60) #every 60 days
fmt <- "%b-%d" # format for axis labels
labs <- format(tt[ix], fmt)
axis(side = 1, at = tt[ix], labels = labs, cex.axis = 0.7)
plot.zoo uses the axis functions in R's classic graphics but zoo also offers lattice graphics as well via xyplot.zoo. Just changing plot to xyplot may be sufficient for your needs:
library(zoo)
library(lattice)
# create test data
z <- zooreg(1:83, start = as.Date("2009-04-01"), deltat = 7)
xyplot(z)
Note that there are further examples in ?plot.zoo and ?xyplot.zoo as well as the three vignettes that come with zoo. In those places you can also find find examples of a different approach showing how to use the axis function of classic graphics together with plot.zoo for highly customized axes.
I have captured all the above and a couple of extra options in one place, for my own reference:
# Time series plots with good X axis labels
library(zoo)
# data
today = Sys.Date()
dates = as.Date((today-500):today)
z = zoo (100+cumsum(rnorm(501)), dates)
# method1 : default X axis labels do not look good
?plot.zoo
plot(z)
?plot.ts
plot(ts(z))
# method 2 : Lattice
library(lattice)
?xyplot.zoo
xyplot(z)
xyplot(z, lwd=2, col="tomato")
# method 3 : XTS
library(xts)
?plot.xts
plot(as.xts(z))
plot(as.xts(z), auto.grid=F, major.format="%b %y", las=2)
# method 4 : Base graph
timeline = time(z)
summary(timeline)
index = seq(from=1, to=length(timeline), 90) # put ticks every 90 days
plot(z, xaxt="n")
axis(side=1, at=timeline[index], label=format(timeline[index], "%b %y"), cex.axis=0.8)
# method 5 : ggplot
library(ggplot2)
library(scales)
?date_breaks
df = data.frame(date=as.POSIXct(time(z)), value=as.numeric(z))
head(df)
# default plot
ggplot(df, aes(x=date, y=value)) + geom_line()
# formatted
ggplot(df, aes(x=date, y=value)) + geom_line() +
scale_x_datetime(labels=date_format("%b '%y"))
# custom breaks
ggplot(df, aes(x=date, y=value)) + geom_line() +
scale_x_datetime(labels=date_format("%b '%y"), breaks=date_breaks("3 months"))
if the time is in Date format, this might be helpful.
ggplot(data_frame, aes(date,column)) + geom_point() +
ggtitle("my title")+
scale_x_date(date_breaks = "1 month",date_labels = "%b") + xlab("month") +
ylab("y_axis title")

Resources