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))
Related
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
I have the current code:
library(plotrix)
upperlimit = c(6.77, 26.79, 29.29, 28.98)
lowerlimit = c(-7.31, 3.85, 4.13, 2.10)
mean = c(-0.27, 15.32, 16.71, 15.54)
df = data.frame(cbind(upperlimit,lowerlimit,mean))
plot(df$mean, ylim = c(-10,30), xlim = range(1,4))
plotCI(df$mean,y=NULL, uiw=df$upperlimit-df$mean, liw=df$mean-df$lowerlimit, err="y", pch=20, scol = "black", add=TRUE)
abline(a= 0, b= 0, col="red", lty=3)
This gives me a boxplot with confidence intervals:
However, I would like the horizontal axis to display "noon", "3pm", "6pm", "9pm" instead. Is this possible? (Or do I have to use ggplot)?
I tried to modify the line from xlim = range(1,4) to xlim = c("noon", "3pm", "6pm", "9pm"), but it didn't work.
Just turn off the default x axis (with xaxt="n") and draw your own (specifying whatever labels= you want)
plot(df$mean, ylim = c(-10,30), xlim = range(1,4), xaxt="n")
plotCI(df$mean,y=NULL, uiw=df$upperlimit-df$mean, liw=df$mean-df$lowerlimit, err="y",
pch=20, scol = "black", add=TRUE)
abline(a= 0, b= 0, col="red", lty=3)
axis(side=1, at=1:4, labels=c("noon", "3pm", "6pm", "9pm"))
I need to put 2 columns of plots side by side (only one row in the example) and I can't manage to control the size of the plots and the position of the labels so that both plots are exactly aligned. This is the code the have now:
split.screen(c(1,2))
screen(1)
par(oma=c(0,1,0,0), mai=c(0.6,0.36,0.5,0.7), cex=0.5, mgp = c(0.5,0.1, 0), tck = -0.05)
plot(datos$UN.CJF*1000, datos$Methane.Produced.CJF, pch = 16, cex = 0.5, col ="black",
xlab = "UN (g/d)", ylab = expression('CH'[4]*'(g/d)'))
title(main = "a)", cex=0.8, line=0.5, adj=0, cex.lab=1.2)
datos$LWchangeD.CJF <- datos$LWchange.CJF/15
screen(2)
par(oma=c(0,1,0,0), mai=c(0.6,0.36,0.5,0.7), cex=0.5, mgp = c(0.5,0.1, 0), tck = -0.05)
scatter2D(datos$UN.CJF*1000, datos$Methane.Produced.CJF,
pch = 16,
xlab = "UN (g/d)", ylab = "",
colvar = datos$LWchangeD.CJF, clab = c("Liveweight change (kg/d)")
)
title(main = "b)", cex=0.8, line=-0.7, adj=0, cex.lab=1.2)
Notice how the size of the 2 figs is different
There must be a better way to do it.
Any help would be MUCH appreciated
Alvaro
grid.arrange did the trick, but I first re-did all my plots in ggplot2.
Thanks a lot for your suggestions
Alvaro
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")))
My problem concerns the making of a graph for a publication in R. I have used the plot function like follows:
plot(x=data$SL, y=data$BD, xlab = "SL (mm)", ylab = "BD (mm)", pch=data$pch)
SL ranges from 51.7 to 73.7 and BD from 13.5 to 20.4. Unfortunately I am not allowed to post images yet.
However, wanting to get rid of the box I used "axes=F". Problem now is lack of control over the axis function. I used:
axis(side=1, lwd=3, xpd=TRUE, at=c(min(data$SL):max(data$SL)))
axis(side=2, lwd=3, xpd=TRUE, at=c(min(data$BD):max(data$BD)))
Problem is that I can't manage to get the y- and x-axis to come together on the same point as in the plot with the box. How to let the x- and y- axis to touch each other?
Most likely setting xaxs = "i" and yaxs = "i" will help you getting the desired behaviour.
plot(c(1,2,3),c(2,4,6),axes=F,xaxs = "i",yaxs="i",xlim=c(0,3),ylim=c(0,6))
axis(side=1, lwd=3, xpd=TRUE, at=0:3)
axis(side=2, lwd=3, xpd=TRUE, at=seq(0,6,2))
Try box(bty='L') to draw only the left and bottom parts of the box. You could also just draw the lines yourself using lines, segments, or abline and using grconvertX and grconvertY functions to find the locations where to draw the lines.
I suggest that you follow the procedure you outlined and then use:
box(which = "plot", bty = "l")
e.g.:
plot.new()
plot.window(xlim = c(1, 18), ylim = c(2, 20))
points(1:18, 2:19, pch = 1, col = "#FF7F24", cex = 1.2)
lines(1:18, 2:19, col = "#FF7F24", lwd = 2)
axis(side = 1,
lwd = 0,
lwd.ticks = 1,
at = 1:18,
cex.axis = 0.9)
title(main = "Plot",
ylab = "Y-Axis")
legend("top",
legend = c("Legend"),
col = c("#FF7F24"),
text.col = c("#FF7F24"),
pch = 1,
bty = "n",
cex = 1.2)
axis(side = 2,
lwd = 0,
lwd.ticks = 1)
box(which = "plot", bty = "l")
You should pass the options lwd = 0 and lwd.ticks = 1 to your seperate axis() calls in order to prevent some parts of your axes to appear fatter than other parts of your axis because some get overlayed by your call to box() and some do not.
The solution of using box() at the end is, I think, more general in that you can use it when e.g. you cannot or do not want to pass bty = "l" in your plot.default or plot.window call.