Remove y-axis label from plot of an xts object - r

Here is the code generating a plot of an xts object:
require("quantmod")
getSymbols("SPY")
plot(Cl(SPY))
Which yields the following plot:
Can you remove the y-axis values (the prices) from a plot of an xts object?
Hint: passing yaxt='n' doesn't work.

Removing the y-axis is easy, but it also removes the x-axis. A couple options:
1) Easy -- use plot.zoo:
plot.zoo(Cl(SPY), yaxt="n", ylab="")
2) Harder-ish -- take pieces from plot.xts:
plot(Cl(SPY), axes=FALSE)
axis(1, at=xy.coords(.index(SPY), SPY[, 1])$x[axTicksByTime(SPY)],
label=names(axTicksByTime(SPY)), mgp = c(3, 2, 0))
3) Customize-ish -- modify plot.xts so axes= accepts a vector of axes to plot and/or TRUE/FALSE.

Adding to Joshua's answer, to modify plot.xts(), all you need to do is alter the following section:
if (axes) {
if (minor.ticks)
axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB")
axis(1, at = xycoords$x[ep], labels = names(ep), las = 1,lwd = 1, mgp = c(3, 2, 0))
#This is the line to change:
if (plotYaxis) axis(2)
}
and obviously you add the parameter plotYaxis=TRUE to the function definition.

You can also try specifying that the x and y as labels are empty, or contain no values/characters. Try using the term xlab="" in your plot command:
e.g. plot(beers,ranking,xlab="",ylab="")
By not including anything between the quotation marks, R doesn't plot anything.
Using this command, you can also specific the labels, so to make the label for the x axis
'beer', use the term xlab="beer".

Related

Matplot R manually labelling x-axis

I have a problem labelling my matplot x-axis row. So I have 1388 instances, but I want my X-axis to a custom labeling in the form of a sequence in dates.
My R code looks like this:
Dates <- seq(as.Date("2004/01/29"), as.Date("2009/07/31"), by = "quarter")
matplot(seq(1388), t(Alldata[1, ]), type = "l",
lwd = 1, lty = 1, col = 1:10, xlab = "Dates", ylab = "Strike",
main = paste("TEST"), xaxt='n')
axis(side = 1:23, at=1, labels = paste(Dates, 1:23))
Can anybody help me get the Dates into the x-axis?
I have tried using same method as this: Change axis labels with matplot in R
but it doesn't work.
AllData is from an excel file in which the first number of rows looks like this:
I think you have confused the way the function axis works. In answering below I will generate a random matrix to replace your Alldata which I don't have access to
Alldata <- t(as.matrix(rnorm(23)))
We can generate the plot again:
matplot(seq(23), Alldata[1, ], type = "l",
lwd = 1, lty = 1, col = 1:10, xlab = "Dates",
ylab = "Strike", main = paste("TEST"), xaxt='n')
Now, its import to know what the arguments to axis are. First
side this is literally the side of the the rectangle on which the plot is drawn. It is one of the numbers 1, 2, 3, 4 corresponding to bottom, left, top, right, respectively.
You want the axis to be on the bottom so we set this to 1.
Next, the at argument, is for where the tick marks should be drawn. So if you have 10 points on your line, and you set this value to 1:10 it will draw a tick mark at each point on the axis. If you set it to c(2,4,6,8,10) it will draw a mark at every second point on the axis. In your case you've set it to 1, which would draw only one tick. Although since the side was set to 1:23 none showed up.
labels This argument will label the ticks which are drawn. Ideally it should be a vector the same length as the at value. You can make sure that they are the same length by creating an index variable and using this as the at variable and to index the labels.
This gets us to:
index <- c(1,7,14,21)
axis(side = 1, at = index, labels = paste(Dates, 1:23)[index])
I think having a full range of dates would look cluttered. But you can drop the index and choose the below if you prefer:
axis(side = 1, at = 1:23, labels = paste(Dates, 1:23))

Plotting in R using plot function

I am trying to plot few graphs using loops. I am now describing in details.
First I have a function which is calculates the y-variable (called effect for vertical axis)
effect<- function (x, y){
exp(-0.35*log(x)
+0.17*log(y)
-0.36*sqrt(log(x)*log(y)/100))
}
Now I run the following code and use the option par to plot the lines in the same graph. I use axis=FALSE and xlab="" to get a plot without labels. I do this so that my labels are not re-written each time the loop runs and looks ugly.
for (levels in seq(exp(8), exp(10), length.out = 5)){
x = seq(exp(1),exp(10), length.out = 20)
prc= effect(levels,x)
plot(x, prc,xlim = c(0,max(x)*1.05), ylim=c(0.0,0.3),
type="o", xlab = "",ylab = "", pch = 16,
col = "dark blue", lwd = 2, cex = 1, axes = F)
label = as.integer(levels) #x variable
text(max(x)*1.03,max(prc), label )
par(new=TRUE)
}
Finally, I duplicate the plot command this time using the xlab and ylab options
plot(x, prc, xlab = "X-label", ylab = "effect",
xlim = c(0,max(x)*1.05), ylim = c(0,0.3),
type="l", col ='blue')
I have several other plots in the similar lines, using complex equations. I have two questions:
Is there an better option to have the same plot with smoother lines?
Is there an easier option with few lines to achieve the same, where I can place the texts (levels) for each line on the right with white background at the back?
I believe working with the plot function was tedious and time consuming. So, I have finally used ggplot2 to plot. There were several help available online, which I have used.

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)

How do I hide axes in a plot of means?

I want to omit the x-axis labels for my plot of means, but I just fail (I didn't use packages such as ggplot2):
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="")) # normal code
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",frame=F)) # which gives no response
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",axes=F))
This gives the following error:
Error in plot.default(c(1, n.levs), yrange, type = "n", xlab = xlab, ylab = ylab, :
formal argument "axes" matched by multiple actual arguments
The below code temporarily sets the color of axis tick labels (i.e., for both the x and y-axis) to "transparent" using par, thus preventing the automatically drawn tick labels from being visible. The y-axis tick labels can easily be inserted afterwards by reverting the par-related modifications.
## sample data
library(Rcmdr)
data(Moore)
## create plot with transparent (i.e., non-visible) tick labels
par(col.axis = "transparent")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
## add y-axis tick labels
par(col.axis = "black")
axis(side = 2, at = seq(0, 25, 5), tick = FALSE)
Remember that this is a rather undesired behavior of plotMeans since the help pages explicitly say that ... are arguments to be passed on to plot. Therefore, if your target is to entirely disable the drawing of the frame and axes when using plotMeans, e.g. similar to the visual output of plot(1:10, 1:10, axes = FALSE), you should probably file a bug report.
Update
In case I understood it wrongly and you actually wanted to remove both the tick labels and the ticks, you could simply use
par(xaxt = "n")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
See also ?par for further possible modifications of R base plots.

Replace X-axis with own values

I have a question regarding the command plot().
Is there a way to fully eliminate the x-axis and replace it with own values? I know that I can get rid of the axis by doing
plot(x,y, xaxt = 'n')
and then add an axis with
axis(side = 1 etc.)
However, when I add the axis, obviously it still refers to the data plotted as 'x'.
I would only like to plot the 'y'-values and add the x-axis my own in the sense of just "drawing" the x-axis with own values specified. Is there any way to do that?
The background of this question is that my two data frames differ in their length and therefore I cannot plot them.
Not sure if it's what you mean, but you can do this:
plot(1:10, xaxt = "n", xlab='Some Letters')
axis(1, at=1:10, labels=letters[1:10])
which then gives you the graph:
You could set labels = FALSE inside axis(...) and then print the labels in a separate command using text(...). This option would allow you to rotate the text in case you need it.
lablist<-as.vector(c(1:10))
axis(1, at=seq(1, 10, by=1), labels = FALSE)
text(seq(1, 10, by=1), par("usr")[3] - 0.2, labels = lablist, srt = 45, pos = 1, xpd = TRUE)
Detailed explanation here

Resources