plot multiple xts objects superimposed with different scales using both vertical axes - r

I have two univariate time series that would like to plot in the same screen chart. The problem is that they have very different scales and therefore the chart becomes very difficult to interpret. How can I plot each series superimposed but each of them using a different vertical axis?
library(xts)
mytime <- as.POSIXlt(seq(Sys.time()-100*60+1,Sys.time(),by=60), origin= '1970-01-01')
x <- xts(rnorm(1:100),mytime)
y <- xts(rnorm(1:100,100,10),mytime)
plot(as.zoo( merge(x,y)), screens=1)

I'm not so sure this is what you want, but here's an idea:
plot(as.zoo(x), las=1)
par(new=TRUE)
plot(as.zoo(y),
col=2,
bty='n',
xaxt="n",
yaxt="n",
xlab="", ylab="")
axis(4, las=1)
legend("topleft",
legend=c("x","y"),
col=1:2,
lty=1,
cex=0.85)

Related

Dual Y-Axis Base R Plot with different x-ticks

I want to make a plot of 4 sets of data points using dual y-axis. The first two are on the left y-axis and last two are on the right y-axis. The first two belong to a set of numbers ranging from 5000 to 50,000. Second two sets of data belong range from 1-100. I want to plot it so that it is easily discernable that the two axis are not only on different scales but also the height between points from the two different sets with distinct ranges is obviously big. I don't want to be able to draw a horizontal line that would suggest some number from the left-y-axis can be mapped bijectively to some number on the right y-axis. I want to make it such that a horizontal line through any points from the left y-axis and right y-axis would belong to only one set related to either left or right axis.
How can I plot with 2 different y-axes?. There's
I'd use twoord.plot
From plotrix v3.7-5
by Jim Lemon but that has the disadvantage than base R beacause I can't add 4 sets of data into one plot. I can only use 2 sets of (x,y) pairs with 2--ord plot. I can theoretically plot n sets of (x,y) pairs using base R.
None
Here's what doesn't work:
time <- seq(0,72,12)
betagal.abs <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35)
cell.density <- c(0,1000,2000,3000,4000,5000,6000)
## add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 6) + 0.1)
## Plot first set of data and draw its axis
plot(time, betagal.abs, pch=16, axes=FALSE, ylim=c(0,1), xlab="", ylab="",
type="b",col="black", main="Mike's test data")
axis(2, ylim=c(0,1),col="black",las=1) ## las=1 makes horizontal labels
mtext("Beta Gal Absorbance",side=2,line=2.5)
box()
## Allow a second plot on the same graph
par(new=TRUE)
## Plot the second plot and put axis scale on right
plot(time, cell.density, pch=15, xlab="", ylab="", ylim=c(0,7000),
axes=FALSE, type="b", col="red")
## a little farther out (line=4) to make room for labels
mtext("Cell Density",side=4,col="red",line=4)
axis(4, ylim=c(0,7000), col="red",col.axis="red",las=1)
## Draw the time axis
axis(1,pretty(range(time),10))
mtext("Time (Hours)",side=1,col="black",line=2.5)
## Add Legend
legend("topleft",legend=c("Beta Gal","Cell Density"),
text.col=c("black","red"),pch=c(16,15),col=c("black","red"))
Not quite sure what you're after but you can add an extra 'line per plot' by using lines.
I've edited your code
## Plot first set of data and draw its axis
plot(time, betagal.abs, pch=16, axes=FALSE, ylim=c(0,1), xlab="", ylab="",
type="b",col="black", main="Mike's test data")
lines(seq(0, 1, 0.02), type = 'o')
axis(2, ylim=c(0,1),col="black",las=1) ## las=1 makes horizontal labels
mtext("Beta Gal Absorbance",side=2,line=2.5)
box()
## Allow a second plot on the same graph
par(new=TRUE)
## Plot the second plot and put axis scale on right
plot(time, cell.density, pch=15, xlab="", ylab="", ylim=c(0,7000),
axes=FALSE, type="b", col="red")
lines(seq(0, 5000, 10), type = 'o', col = 'red')
## a little farther out (line=4) to make room for labels
mtext("Cell Density",side=4,col="red",line=4)
axis(4, ylim=c(0,7000), col="red",col.axis="red",las=1)
which produces this:
Please let me know if this wasn't what you were after.

Removing axes in beeswarm plot

I have a following "beeswarm" (a single-dimensional scatterplot)
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE)
Here is the resulting plot:
How can I get rid of the axes and the box around the plot, so that I can reintroduce only the X axis and nothing else around it?
If you create an empty plot first
plot(rnorm(10), type="n", axes=FALSE, xlim=c(0, 200), ylim=c(0.4, 1.6),
xlab="", ylab="")
Then you can use the add argument to get what you want
beeswarm(breast$time_survival,horizontal=TRUE, add=TRUE)
You can use the "axes" argument (described in ?plot.default).
beeswarm(breast$time_survival, horizontal=TRUE, axes = FALSE)

putting a trend line and r squared value in a graph

I made line graphs of two different scales and i want to draw two trend lines in each line graph and display r squared value in the graph. R squared value of temperature and Year, precipitation and year. I am also having problem on showing legend on the precipitation axis. Please help me fix it.
library(lattice)
data=read.table("temp_pcp.csv",header=TRUE, sep=",")
frame=data.frame(data[1:3])
year <- data[1]
Temperature <- data[3]
Precipitation <- data[2]
gm<-data.frame(year,Temperature)
mg<-data.frame(year,Precipitation)
plot(gm, axes=FALSE, ylim=c(0,20), xlab="", ylab="",
type="l",col="black", main="Climograph")
axis(2, ylim=c(0,20),col="black",las=1)
mtext("Temperature in Degree Celcius ",side=2,line=2.5)
box()
grid()
par(new=TRUE)
plot(mg, xlab="", ylab="", ylim=c(0,60),
axes=FALSE, type="l", col="red")
mtext("Precipitation in mm",side=4,col="black",line=4)
axis(4, ylim=c(0,60), col="black",col.axis="black",las=1)
axis(1,pretty(range(year),10))
mtext("Year",side=1,col="black",line=2.5)
legend("topleft",legend=c("Tempreature","Precipitation"),
text.col=c("black","red"),col=c("black","red"))
The line graph i created is as follow;

R horizontal barplot with aligned plot ontop

I am having trouble getting the spacing right on a plot on top of a horizontal barplot. It is the same general issue as described here:
http://www.r-bloggers.com/adding-lines-or-points-to-an-existing-barplot/
But I am trying to use "plot" instead of "points" or "lines". Is there a trick for using plot to get the spacing of the bars and the points to match?
Code:
barplot(df$DIC_mM,col=scalegreen, xlab="DIC mM", horiz=TRUE, xlim=c(0,0.7),
col.axis="white", col.lab="white", axes=FALSE, border="white")
axis(1,line=1,col="white",col.ticks="white",col.axis="white")
par(new = TRUE)
plot(df$d13DIC,df$Order, type="p", axes = FALSE, bty = "n", xlab ="",
col="deepskyblue2", lwd=5, xlim=c(-50,170), lend=2, col.lab="white", ylab="")
axis(3,at = c(-50,0,50,100,150), line=1, col="deepskyblue2", col.ticks="deepskyblue2",
col.axis="deepskyblue2")
mtext(expression(paste(delta ^{13},'DIC'," \u0028","\u2030","\u0029")), 3,
line=-0.5,at=50,col="deepskyblue2", cex=0.75)
Is there a reason why you don't want to use points to add the points? If you're willing to use points you can do it like this:
Create barplot and save the y-coordinates of the bars to y. You haven't provided sample data, so I'll use the built-in mtcars data frame:
y = barplot(mtcars$mpg[1:10], horiz=TRUE)
Now add the points. We use y as the y values, because those are the coordinates of the midpoints of each bar:
points(sqrt(mtcars$mpg[11:20]), y, col="red", pch=16, cex=2)
When you use par(new=TRUE) and then call plot again, you're overlaying a new plot with a new coordinate system that in general will be different from the original coordinate system.
This is what worked, based on this post suggested by eipi10: midpoints returned by barplot function do not actually line up with midpoints of bars
mp<-barplot(df$DIC_mM,col=scalegreen, xlab="DIC mM", horiz=TRUE, xlim=c(0,0.7), col.axis="white", col.lab="white", axes=FALSE, border="white", ylim=c(0,length(df$DIC_mM)+2))
axis(1,line=1,col="white",col.ticks="white",col.axis="white")
par(new = TRUE)
plot(df$d13DIC, mp, type="p", axes = FALSE, bty = "n", xlab ="",col="deepskyblue2", lwd=5, xlim=c(-50,170), lend=2, col.lab="white", ylab="", ylim=c(0,length(df$DIC_mM)+2))
axis(3,at = c(-50,0,50,100,150),line=1,col="deepskyblue2",col.ticks="deepskyblue2",col.axis="deepskyblue2")
mtext(expression(paste(delta ^{13},'DIC'," \u0028","\u2030","\u0029")),3,line=-0.5,at=50,col="deepskyblue2", cex=0.75)

How to reduce the marker symbol in a line plotted by using R?

I am using R to plot line chart, with the following command
data <- read.table("input_data.txt", header=T, sep="\t")
ind=seq(1,nrow(data),by=2)
pdf(file="result.pdf")
plot_colors <- c("black","red","green","blue","purple","red")
plot(data$column_one, type="l", lty=1, col=plot_colors[1], ann=FALSE)
lines(data$column_two, type="l", lty=2, col=plot_colors[2])
lines(data$column_three, type="o", pch=1, lty=0, col=plot_colors[3], cex=1)
lines(data$column_four, type="o", pch=3, lty=0, col=plot_colors[4], cex=1)
lines(data$column_five, type="o", pch=2, lty=0, col=plot_colors[5], cex=1)
lines(data$column_six, type="o", pch=4, lty=1, col=plot_colors[6], cex=1)
box()
dev.off()
The problem is, I have 500 data points, and the symbol markers are all mashed up on the line, tightly compact on the line. I could not see the symbols on the line.
Is there a way to just show the symbol markers at fixed interval, without them cluttering together?
Use something like that:
lines(data$column_three, type="o", pch=c(1,NA,NA,NA,NA), lty=0, col=plot_colors[3], cex=1)
For the pch command: The number (here "1") defines your symbol as before. The "NA" means, that these points are plotted with no symbol. This vector will be repeatedly used until the end of your plot. Here, every 5th point is plotted with symbol 1.

Resources