X axis on a two-screen zoo plot - r

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

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

R barplots: specify intervals of date-based x-axis

I've been producing different sets of charts, all in R base. I have a problem though with barplots. I've formatted the x-axis to show the dates by year, however, many years show up several times. I would like each year to only show up once.
Here's my example code:
library(quantmod)
start <- as.Date("01/01/2010", "%d/%m/%Y")
#Download FRED data
tickers <- c("WTISPLC", "DCOILBRENTEU")
fred <- lapply(tickers, function(sym) {na.omit(getSymbols(sym, src="FRED", auto.assign=FALSE, return.class = "zoo"))})
df <- do.call(merge, fred)
#Subset for start date
df <- subset(df, index(df)>=start)
#Create bar plot
par(mar = c(5,5,5,5))
barplot(df[,2], names.arg=format(index(df), "%Y"), ann=FALSE, bty="n", tck=-0, col=1:1, border=NA, space=0); title(main="Example chart", ylab="y-axis")
This example should be reproducible and show clearly what I mean. Now, I've been researching how to add a separate x-axis and how to define that axis. So, I've tried to add the following code:
#Plot bars but without x-axis
barplot(df[,2], names.arg=format(index(df), "%Y"), ann=FALSE, bty="n", tck=-0, xaxt="n", col=1:1, border=NA, space=0); title(main="Example chart", ylab="y-axis")
# Set x-axis parameters
x_min <- min(index(df))
x_max <- max(index(df))
xf="%Y"
#Add x-axis
axis.Date(1, at=seq(as.Date(x_min), x_max, "years"), format=xf, las=1, tck=-0)
This does not give me an error message, but it also does absolutely nothing in terms of drawing an x-axis.
Please do not provide a solution for ggplot. Even though I like ggplot, these barplots are part of a bigger project for me, all using R base and I would not like to introduce ggplot into this project now.
Thanks!
If you are not limited to barplot, you may use the following very simple solution using plot.zoo behind the screens:
# only use what you want, and avoid multiple plots
df2 <- df[ , 2]
# use zoo.plot's functionality
plot(df2, main = "Example Chart", ylab = "y-axis", xlab = "")
This yields the following plot:
I know it is not a barplot, but I don't see what a barplot would add here. Please let me know, whether this is what you want or not.
Edit 1
If you do want to use barplot you may use the following code:
### get index of ts in year format
index_y <- format(index(df), "%Y")
### logical vector with true if it is the start of a new year
index_u <- !duplicated(index_y)
### index of start of new year for tick marks
at_tick <- which(index_u)
### label of start of new year
labels <- index_y[index_u]
### draw barplot without X-axis, and store in bp
### bp (bar midpoints) is used to set the ticks right with the axis function
bp <- barplot(df[,2], xaxt = "n", ylab= "y-axis")
axis(side = 1, at = bp[at_tick] , labels = labels)
yielding the following plot:
Please let me know, whether this is what you want.
Edit 2
We need to take into account two bits of information, when explaining why the ticks and labels group together at the left-hand side.
(1) in barplot, space defines the amount of space before each bar (as a fraction of the average bar width). In our case, it defaults to around zero (see ?barplot for details). In the illustration below, we use spaces of 0.0, 0.5, and 2.0
(2) Barplot returns a numeric vector with the midpoints of the bars drawn (again see the help pages for more detailed info). We can use these midpoints to add information to the graph, like we do in the following excerpt: after storing the result of barplot in bp, we use bp to set the ticks: axis(... at = bp[at_tick] ... ).
When we add space, the location of the bar midpoints change. So, when we want to use the bar midpoints after adding space, we need to be sure we have the right information. Simply stated, use the vector returned by barplot with the call where you added space. If you don't, the graph will be messed up. In the below, if you continue to use the bar-midpoints of the call with (space=0), and you increase space, the ticks and labels will group at the left-hand side.
Below, I illustrate this with your data limited to 3 months in 2017.
In the top layer 3 barplots are drawn with space equal to 0.0, 0.5 and 2.0. The information used to calculated the location of ticks and labels is recalculated and saved at every plot.
In the bottom layer, the same 3 barplots are drawn, but the information used to draw the ticks and labels is only created with the first plot (space=0.0)
# Subset for NEW start for illustration of space and bp
start2 <- as.Date("01/10/2017", "%d/%m/%Y")
df2 <- subset(df, index(df)>=start2)
### get index of ts in month format, define ticks and labels
index_y2 <- format(index(df2), "%m")
at_tick2 <- which(!duplicated(index_y2))
labels2 <- index_y2[!duplicated(index_y2)]
par(mfrow = c(2,3))
bp2 <- barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 0.0, main ="Space = 0.0")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
bp2 <- barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 0.5, main ="Space = 0.5")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
bp2 <- barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 2.0, main ="Space = 2.0")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
### the lower layer
bp2 <- barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 0.0, main ="Space = 0.0")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 0.5, main ="Space = 0.5")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 2.0, main ="Space = 2.0")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
par(mfrow = c(1,1))
Have a look here:
Top layer: bp recalculated every time
Bottom layer: bp space=0 reused
Cutting and pasting the commands in your console may illustrate the effects better than the pic above.
I hope this helps.
You could use the axis function, I used match to obtain the indices of the dates on the axis:
space=1
#Plot bars but without x-axis
barplot(df[,2], names.arg=format(index(df), "%Y"), ann=FALSE, bty="n", tck=-0, xaxt="n",
col=1:1, border=NA, space=space); title(main="Example chart", ylab="y-axis")
# Set x-axis parameters
x_min <- min(index(df))
x_max <- max(index(df))
#Add x-axis
axis(1, at=match(seq(as.Date(x_min), x_max, "years"),index(df))*(1+space),
labels = format(seq(as.Date(x_min), x_max, "years"),"%Y"),lwd=0)
Hope this helps!

R barchart, not barplot, remove yaxis ticks

I am trying to remove the y-axis ticks from a barchart.
google immediately corrects barchart to barplot, and is not helpful at all.
yaxt="n" is not working with barchart...
Anyone have an idea how to remove the yaxis ticks in barcharts in R?
I need barchart because this is the only way I could find that works for me to group my data in the way I want it...
An MWE is here:
library(lattice)
molnames<-c("A","B","D","G","C","F")
contactcounts<-c(1,2,3, 6,12,18,4,8,16,10,20,30,2,4,8,3,6,9)
Acolumn1=factor(rep(molnames, each=3), levels=molnames )
Acolumn2=rep(c("test1","test2","test3"), 6)
Acolumn3=contactcounts
colour<-c("orange", "blue","magenta")
tiff(file="./testingABC.tiff", res=1000, width = 8, height = 8,units='in')
trellis.par.set("grid.pars"=list(fontfamily="serif"))
barchart(Acolumn3 ~ Acolumn1,ylab="y axis", yaxt="n", groups=Acolumn2, auto.key = list(columns = 3), par.settings=list(superpose.polygon=list(col=colour)))
Please note that you're using a function from lattice package, not from base package and it has different parameters.
To accomplish what you want, you should set scales parameter (see ?barchart documentation); there are two options giving a slighlty different result :
# option 1: we're saying that y ticks must be set at coordinate = NULL
barchart(Acolumn3 ~ Acolumn1,ylab="y axis", groups=Acolumn2, auto.key = list(columns = 3),
scales=list(y=list(at=NULL)),
par.settings=list(superpose.polygon=list(col=colour)))
# option 2: we're saying not to draw y axis
barchart(Acolumn3 ~ Acolumn1,ylab="y axis", groups=Acolumn2, auto.key = list(columns = 3),
scales=list(y=list(draw=FALSE)),
par.settings=list(superpose.polygon=list(col=colour)))
Here's an example how to do a barplot using base R:
# Acolumn1,Acolumn2,Acolumn3 have been created in your example
DF <- data.frame(Acolumn1,Acolumn2,Acolumn3)
###### build matrix to be passed to barplot using R base
reshaped <- reshape(DF, idvar="Acolumn1",timevar="Acolumn2", direction = "wide",sep='_')
names(reshaped) <- gsub('Acolumn3_','',names(reshaped))
reshapedMx <- as.matrix(reshaped[,-1])
rownames(reshapedMx) <- reshaped[,1]
reshapedMx <- t(reshapedMx)
###### build matrix to be passed to barplot using reshape2 package (less code)
# library(reshape2)
# reshapedMx <- acast(DF, Acolumn1 ~ Acolumn2, value.var='Acolumn3')
# reshapedMx <- t(reshapedMx)
colors <- rainbow(nrow(reshapedMx))
barplot(reshapedMx,beside = TRUE,col=colors,ylim=c(0,max(reshapedMx)*1.2), yaxt='n')
legend('top',fill=colors,legend=rownames(reshapedMx), horiz=TRUE)
# call box() if you want to add a box around the plot

dates ticks and labels on x-axis of a time series

I am trying for the last few hours to get the ticks of desired dates with labels as Month-Year on the x-axis of a time series plot. I have tried tons of things available on Stack and others but none worked out so far.
Below is an example of what I am trying so far. I am getting plot with x-axis as a numeric such as 2014.0, while I want it to be in the date format such as Jan-2014.
I am also trying to learn, in case of numeric year labels, how can I start my x-axis with 2014.1 instead of 2014.0 as the first month of my series is January. To get this I tried time function with offset=1 but it didn't work either. Please check below example
## dataframe and its time series
temp_df<- data.frame(date_temp= as.Date(dates_of_data), opp_temp= rnorm(29,mean = 100,sd=5))
temp_ts<- ts(temp_df[,2], start=2014, freq=12)
## plot
plot(temp_ts, axes=F, lwd=3, ylim=c(min(temp_ts),max(temp_ts)), xlab="", ylab="",type="l",col="black", main="")
points(temp_ts,pch=20,col="yellow")
## y-axis
axis(2, ylim=c(min(temp_ts),max(temp_ts)),col="black",lwd=2,line=1)
mtext(2,text="Y-axis count",line=3,col="black")
## x-axis
axis(1,pretty(range(time(temp_ts)),12))
mtext("Time - Year",side=1,col="black",line=2, lwd=3)
## axis(1,pretty(range(time(temp_ts, offset=1)),12)) -- didnt work either
temp_dates<- as.Date(as.yearmon(time(temp_ts)))
axis(side=1, at=tt, labels = FALSE)
axis(side = 1, at = tt[ix], labels = labs[ix], tcl = -0.7, cex.axis = 1)
grid (NULL,NULL, lty = 6, col = "blue")
dev.off()
Couple of things that I have tried so far includes
# 1. par(xaxt="n") ## didn't work
# 2. axis(1, at=seq(from = min(temp_dates),to = max(temp_dates), by="month"), labels=format(temp_dates,"%Y-%b"),las=2)
Can you please tell me how to get x-axis labels as Jan-2014 (Month-Year)?
These are some of the link I went through as requested: here
here
here
here
and
here
EDIT: Below solution works perfectly using zoo library. However I have't been using zoo the whole time in my study and was interested more in doing the other way. Please correct what is wrong in the previous approach.
require(zoo)
dev.off()
x<- (zoo(temp_df$opp_temp, temp_df$date_temp))
plot(x, xaxt = "n")
x_times <- time(x)
ticks <- seq(x_times[1], x_times[length(x_times)], by = "month")
grid (5,5, lty = 6, col = "blue")
axis(1, at = ticks, labels = format(x_times,"%Y-%b"),las=2, tcl = -0.3)

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