R - Dual y axis chart : align 0 and adjust scales - r

I used plot.new to combine two different graphs on R with this code:
par(mar=c(4, 4, 3, 5))
plot(d1, col="grey", yaxt="n", xlab="", ylab = "", ylim(0, 1.5), type = "h", fill ="grey")
axis(4, at=seq(0,1.5, by=0.05), col="grey", col.axis="grey")
mtext("Fn_1", side=4, line=2.5, col="grey")
mtext("Sqrt Insolation", side=1, line=2.5, col="black")
par(new=TRUE)
plot(h, axes=FALSE, type = "l", xlab = "", ylab="")
axis(2, ylim=c(-0.10, 0.1), at=seq(-0.10, 0.1, by=0.05), col="black", col.axis="black") mtext("Fn_2", side=2, line=2.5, col="black")
This is my result
https://i.stack.imgur.com/umCWu.png
I want to have a y axis like ylim = c (-0.10,1.5) and adjust the scale (h curb is way too big with this scale ..)
OR two y axes like here, but align on 0 with the same scale.
I tried to change the criteria of my axes but they seem to adjust automatically to make sense with the minimum and maximum of my data. I want to have one y axis as ylim = c(-0.10,1.5) and adjust scale (h courb is way too large with this actual scale..)
Can someone help me ?
Thank you very much

Related

How to shift bars from y-axis using barplot() R

I have a barplot with the following code:
bp <- barplot(COL0.matrix,
beside=T,
col=col,
ylim=c(0,100), yaxt="n",
xlab="Time",ylab="Relative Electrolyte Leakage (%)",
las=1,xaxt = "n",
cex.axis=1.5, cex.names= 1.5, font=2, font.lab=2, cex.lab=1.5, family="A", space=c(0,0,1,0), xaxs = 'i')
axis(side=2, family="A", cex.axis=0.8, las=1, font=2, pos=0, tck=c(0), at=c(0,10,20,30,40,50,60,70,80,90,100), labels=c("0", "10","20","30","40","50","60","70","80","90","100"))
axis(side=2, at=c(0,10,20,30,40,50,60,70,80,90,100), labels = c(NA),tcl=c(-0.25),pos=0)
axis(side=2, at=c(0,10,20,30,40,50,60,70,80,90,100), labels = c(NA),tcl=c(0.25),pos=0)
axis(side=1, at=c(1.2, 4.2), labels = c("Dawn", "Dusk"),tck=c(0), family="A", cex.axis=1.5, font=2, pos=0)
This results in the following barplot:
I am trying to shift the bars which are right next to the y-axis away. I have tried changing space=(...) but this shifts the whole x-axis so that the x and y axis no longer join.
Is there a way of shifting the left two bars over?
You can use the line parameter to move the axis over instead of moving the bars. You want to remove the pos = 0 and define the y title outside the barplot function so you can also control its position. Also you will want to play with the par(mar = ... part so it looks right for your device. For if you save in a pdf device your margin and even the cex parameters probably will need adjusting to make it nice. Also I set the graphics parameter xpd = TRUE to allow the lines function in the last line to plot into the margin space. If you don't do that you'll have a x axis that doesn't meet the y axis. If you don't want that then remove the last line.
COL0.matrix <- structure(c(71.44109964, 78.43178612, 64.31581642, 70.3339388 ), .Dim = c(2L, 2L), .Dimnames = list(c("Control", "bold(\"Col-0 840g ha\"^\"-1\")" ), c("Dawn", "Dusk")))
col = c("white", "grey70", "white", "grey70")
par(mar = c(5,7,5,5), xpd = TRUE)
bp <- barplot(COL0.matrix,
beside=T,
col=col,
ylim=c(0,100), yaxt="n",
xlab="Time", ylab = "",
las=1,xaxt = "n",
cex.axis=1.5,
cex.names= 1.5,
font=2,
font.lab=2,
cex.lab=1.5,
family="A",
space=c(0,0,1,0),
xaxs = 'i')
mtext("Relative Electrolyte Leakage (%)", side = 2, font = 2, cex = 1.5, line = 4)
axis(side=2, family="A", cex.axis=0.8,
las=1, font=2, tck=c(0),
at=c(0,10,20,30,40,50,60,70,80,90,100),
labels=c("0", "10","20","30","40","50","60","70","80","90","100"),
line = 1)
axis(side=2, at=c(0,10,20,30,40,50,60,70,80,90,100), labels = c(NA),tcl=c(-0.25), line = 1)
axis(side=2, at=c(0,10,20,30,40,50,60,70,80,90,100), labels = c(NA),tcl=c(0.25), line = 1)
axis(side=1, at=c(1.2, 4.2), labels = c("Dawn", "Dusk"),tck=c(0), family="A", cex.axis=1.5, font=2, line = 0)
lines(x = c(-0.3, 5.3), y = c(0, 0))

How to avoid wired ylab error when plotting in R

I need a two y-axes figure. hrbrmstr suggested to use simple plots. But when adapting the graph to my setting I observed I cannot add the ylab on the right hand side, getting a wired error:
Error in axis(4, ylim = c(0, 1), col = "black", col.axis = "black", las = 1, :
'labels' is supplied and not 'at'
Is this avoidable?
look at the code the bottom line fpr SOURCE OF ERROR
featPerf <- data.frame( expS=c("1", "2", "3", "4"),
exp1=c(1000, 0, 0, 0),
exp2=c(1000, 5000, 0, 0),
exp3=c(1000, 5000, 10000, 0),
exp4=c(1000, 5000, 10000,20000),
accuracy=c(0.4, 0.5, 0.65, 0.9) )
# make room for both axes ; adjust as necessary
par(mar=c(5, 5, 5, 7) + 0.2)
# plot the bars first with no annotations and specify limits for y
#barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", ylim=c(0, max(colSums(featPerf[2:5]))))
barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", beside=TRUE)
# make the bounding box (or not...it might not make sense for your plot)
#box()
# now make the left axis
axis(2, ylim=c(0, max(colSums(featPerf[2:5]))), col="black", las=1)
# start a new plot
par(new=TRUE)
# plot the line; adjust lwd as necessary
plot(x=1:4, y=featPerf[,6], xlab="Experiments", ylab="Abs. # of Features", axes=FALSE, type="l", ylim=c(0,1), lwd=5)
# annotate the second axis -- SOURCE OF ERROR -> VVVVVVVVVVVVVVVVVV
axis(4, ylim=c(0,1), col="black", col.axis="black", las=1, labels="Accuracy")
Like this?
par(mar=c(4,4,1,4) + 0.2)
barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", beside=TRUE)
axis(2, ylim=c(0, max(colSums(featPerf[2:5]))), col="black", las=1)
par(new=TRUE)
plot(x=1:4, y=featPerf[,6], xlab="Experiments", ylab="Abs. # of Features", axes=FALSE, type="l", ylim=c(0,1), lwd=5, col="blue")
axis(4, ylim=c(0,1), col="blue", col.axis="blue", las=1)
mtext("Accuracy",4,line=2, col="blue")
For the record, it is never a good idea to stack plots on top of each other this way (with two axes). I've made the line and the axis the same color in an attempt to draw attention to what you are doing, but this is still a very bad idea.
First of all it is not advisable to use two Y-axes in a same plot.
If you add at argument to the axis call, you get the name "Accuracy" on the right hand side of the plot.
axis(4, ylim=c(0,1), col="black", col.axis="black", las=1, labels="Accuracy",
at = .5)

How to draw single axis plot in R

I wish to draw very very simple line with three numbers. It would be like below
|------|--------------|
0.5 1.5 3.4
Is it too simple to ask?
First, plot nothing, remove the axes, and add an x-axis back in at the specified points:
x <- c(.5, 1.5, 3.4)
plot(0, xlim = c(0, 3.5), axes=FALSE, type = "n", xlab = "", ylab = "")
axis(1, at = x, labels = x)
plot(1:10, rep(0,10), type='b', pch='|', axes=F, xlab="", ylab="", xlim=c(0,10))
text(1:10, rep(-0.1,10), labels=1:10)
margins and plot size can be tweaked with X11 and par
You can do it in grid,
library(grid)
grid.newpage()
grid.xaxis(at=c(0.5, 1.5, 3.4),
vp=vpStack(viewport(height=unit(2,"lines")),
viewport(y=1, xscale = c(0.4, 3.5), just="bottom")))

Two x-axis scales on one R plot

I am trying to create a plot that shows two different time scales on the x-axis. The problem is that the two time scales have a complicated relationship.
I would like to show weather data by the day of year and by the thermal units. Thermal units are the accumulation of the mean temperatures of each day. Some days we get a lot of thermal units, some days not so many. I fit a spline to the relationship between day of year and thermal units and used that to predict thermal unit values for each day. So I do have a nice dataset with the following headers: day of year (day), thermal units (gdd), temperature (temp), precipitation (precip).
I created the following figure (may have to open in new window):
with this code:
pdf(file="Climate 2010.pdf", family="Times")
par(mar = c(5,4,4,4) + 0.3)
plot(cobs10$day, cobs10$precip, col="white", type="h", yaxt="n", xaxt="n", ylab="",
xlab="")
axis(side=3, col="black", labels=FALSE)
at = axTicks(3)
mtext(side = 3, text = at, at = at, col="black", line = 1, las=0)
mtext("Day of Year", side=3, las=0, line = 3)
par(new=TRUE)
plot(cobs10$gdd, cobs10$temp, type="l", col="red", yaxt="n", ylab="", xlab="Thermal
Units")
axis(side=2, col='red', labels=FALSE)
at= axTicks(2)
mtext(side=2, text= at, at = at, col = "red", line = 1, las=0)
mtext("Temperature (C)", side=2, las=0, line=3)
par(new=TRUE)
plot(cobs10$gdd, cobs10$precip, type="h", col="blue", yaxt="n", xaxt="n", ylab="",
xlab="")
axis(side=4, col='blue', labels=FALSE)
at = axTicks(4)
mtext(side = 4, text = at, at = at, col = "blue", line = 1,las=0)
mtext("Precipitation (cm)", side=4, las=0, line = 3)
dev.off()
This is exactly what I want, but I realized the x-axis scales are linear here, and they should not be. I put the top x-axis in by making my precipitation data white and writing over it. See what happens when I make it green:
It's obvious things don't match up. So how can I make the two axes in scale with eachother?
Here's the little dataframe I have been using where the time units are matched up by predicting:
cobs10.txt. "gdd" is thermal units
EDIT: Here is some new code that doesn't use par(new=TRUE):
par(mar = c(5,4,4,4) + 0.3)
plot(cobs10$gdd, cobs10$temp, type="l", col="red", yaxt="n", xlab="", ylab="",
ylim=c(-25, 30))
lines(cobs10$gdd, cobs10$precip, type="h", col="blue", yaxt="n", xlab="", ylab="")
axis(side=3, col="black", at=cobs10$gdd, labels=cobs10$day)
want<-(c(1, 130, 150, 170, 190, 210, 230, 250, 270, 360))
mtext(side = 3, text = want, at = want, col="black", line = 1, las=0)
mtext("Day of Year", side=3, las=0, line = 3)
axis(side=2, col='red', labels=FALSE)
at= axTicks(2)
mtext(side=2, text= at, at = at, col = "red", line = 1, las=0)
mtext("Temperature (C)", side=2, las=0, line=3)
axis(side=4, col='blue', labels=FALSE)
at = axTicks(4)
mtext(side = 4, text = at, at = at, col = "blue", line = 1,las=0)
mtext("Precipitation (cm)", side=4, las=0, line = 3)
It is almost never a good idea to use par(new=TRUE), it causes more problems than it solves.
You should decide which x axis units you want to use, days or thermal units and create the initial graph using that unit, then use functions like points or lines to add any additional points or lines to the existing graph using the existing units. You can then use the axis function to add another axis, use the original units for the at argument, but then the conversion to the other units for the labels (you need to decide if you want the locations of the ticks to match the original units, or be in locations that give pretty values for the converted units).

R: plotting untransformed data on a log x axis (similar to plotting on log graph paper)

I have 3 sets of data that I am trying to plot on a single plot. The first data set x values range from ~ 1 to 1700 whereas the other two data sets x values are less than 20. Therefore I want to plot them on a log axis to show variations in all the data sets. However I do not want to transform the data as I want to be able to read the values off the graph. The x axis labels I would like are 1, 10, 100 and 1000 all equally spaced. Does anyone know how to do this? I can only find examples where the data is log as well as the axis. I have attached the code I am currently using below:
Thanks in advance for any help given.
Holly
Stats_nineteen<-read.csv('C:/Users/Holly/Documents/Software Manuals/R Stuff/Stats_nineteen.csv')
attach(Stats_nineteen)
x<-Max
x1<-Min
x2<-Max
y1<-Depth
y2<-Depth
par(bg="white")
par(xlog=TRUE)
plot(x2,y1, type="n", ylim=c(555,0), log="x", axes=FALSE, ann=FALSE)
box()
axis(3, at=c(1,10,100,1000), label=c(1,10,100,1000), pos=0, cex.axis=0.6)
axis(1, at=c(1,10,100,1000), label=c(1,10,100,1000), cex.axis=0.6)
axis(2, at=c(600,550,500,450,400,350,300,250,200,150,100,50,0), label=c
(600,"",500,"",400,"",300,"",200,"",100,"",0), cex.axis=0.6)
mtext("CLAST SIZE / mm", side=3, line=1, cex=0.6, las=0, col="black")
mtext("DEPTH / m", side=2, line=2, cex=0.6, las=0, col="black")
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "solid",
lwd = par("lwd"), equilogs = TRUE)
par(new=TRUE)
lines(x1,y1, col="black", lty="solid", lwd=1)
lines(x2,y2, col="black", lty="solid", lwd=1)
polygon(c(x1,rev(x2)), c(y1,rev(y2)), col="grey", border="black")
par(new=TRUE)
plot(x=Average,y=Depth, type="o",
bg="red", cex=0.5, pch=21,
col="red", lty="solid",
axes=FALSE, xlim=c(0,1670), ylim=c(555,0),
ylab = "",xlab = "")
par(new=TRUE)
plot(x=Mode,y=Depth, type="o",
bg="blue", cex=0.5, pch=21,
col="blue", lty="solid",
axes=FALSE, xlim=c(0,1670), ylim=c(555,0),
ylab = "",xlab = "")
You can do this in ggplot using scale_x_log
so something like:
myplot <- ggplot( StatsNinetee,
aes (x = myResponse,
y = myPredictor,
groups = myGroupingVariable) ) +
geom_point() +
scale_x_log()
myplot
also, avoid attach() it can give odd behavior.

Resources