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"))
Related
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))
I am using the following couple of lines to produce the below plot from rows of two 4X10 Matrix d1, and d2 in one graph:
plot(as.matrix(d1[2,]), as.matrix(d2[2,]), type="o", col="red",
ann=FALSE, pch=17, log = 'y',lty=4, axes=FALSE, las=2) +
lines(as.matrix(d1[1,]),as.matrix(d2[1,]), type="o", col="blue",
ann=FALSE, pch=15, lty=4)
x_axis_labels <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
axis(1,labels = x_axis_labels, at = x_axis_labels)
y_axis_labels <- c(3e+4, 6e+4,2e+5,3e+5, 6e+5, 2e+6,5e+6)
axis(2,labels = y_axis_labels, at = y_axis_labels, las=2)
grid()
Which produces the following:
But what I like to have is to have the grid to start from all the labels on each axis. At the moment it only starts from some of the labels on the x-axis, and is not aligned with any of the y-axis labels.
This will probably be easier to control with abline:
abline(v=x_axis_labels, h=y_axis_labels, lty=2, col='lightgray')
If you want the gridlines behind points etc., then try the panel.first argument to plot:
plot(as.matrix(d1[2,]), as.matrix(d2[2,]), type="o", col="red",
ann=FALSE, pch=17, log = 'y',lty=4, axes=FALSE, las=2,
panel.first=abline(v=x_axis_labels, h=y_axis_labels, lty=2, col='lightgray'))
I have created a barplot with 24 bars on the x-axis (0-23) and background shading using the following code:
#Data
Hours = seq(from=0, to=23)
Mean = rnorm(24, mean=5, sd=2)
#Create number seq for tick mark locations
at_tick = seq_len(length(Hours)+1)
#Plot with background rectangle shading
x=barplot(Mean,names.arg=Hours, border="white", ylab="Freq", xlab="Hour",
ylim=c(0,10), axes=FALSE, space=0, col="grey50")
X = c(0,5)
Y = c(0,10)
rect(X[1], Y[1], X[2], Y[2], border = "gray80", col = "gray80")
X2 = c(19,24)
Y2 = c(0,10)
rect(X2[1], Y2[1], X2[2], Y2[2], border = "gray80", col = "gray80")
barplot(Mean,names.arg=Hours, ylim=c(0,10), border="white", ylab="", xlab="", axes=FALSE, space=0, col="gray50", add=TRUE)
axis(2, las=2, pos=0)
axis(1, at = at_tick -1, pos=0, labels = FALSE)
box(which="plot", bty="]") #add a box around the plot
This creates a plot with a surrounding box that extends beyond the limits of the x-axis in both directions. Instead, I would like to add a box around the plot that aligns with the axis limits (i.e. x-axis: 0-23, y-axis: 0-10). I have spent ages trying to find a way to do this with no luck. Any help would be really appreciated. Thanks!
How about drawing individual lines? You can use the segment function instead of box to do this:
segments(24,10, 24,0)
segments(0,10, 24,10)
Complete code:
#Data
Hours = seq(from=0, to=23)
Mean = rnorm(24, mean=5, sd=2)
#Create number seq for tick mark locations
at_tick = seq_len(length(Hours)+1)
#Plot with background rectangle shading
x=barplot(Mean,names.arg=Hours, border="white", ylab="Freq", xlab="Hour",
ylim=c(0,10), axes=FALSE, space=0, col="grey50")
X = c(0,5)
Y = c(0,10)
rect(X[1], Y[1], X[2], Y[2], border = "gray80", col = "gray80")
X2 = c(19,24)
Y2 = c(0,10)
rect(X2[1], Y2[1], X2[2], Y2[2], border = "gray80", col = "gray80")
barplot(Mean,names.arg=Hours, ylim=c(0,10), border="white", ylab="", xlab="", axes=FALSE, space=0, col="gray50", add=TRUE)
axis(2, las=2, pos=0)
axis(1, at = at_tick -1, pos=0, labels = FALSE)
segments(24,10, 24,0)
segments(0,10, 24,10)
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")))
x <- seq(0.5, 0.9, length = 400)
y <- dnorm(x,0.7,0.0458)
plot(x, y, type="l", yaxt="n",ann=FALSE,bty="n", xaxt="n")
axis(1, at=seq(0.5,0.9,by=0.1), labels=c("","",0.7, 0.8, 0.9) )
mtext("Proportions", 1, at=0.9, line=2)
xx=c(0.8,seq(0.8,0.9,length=100),0.9)
yy= c(0,dnorm(seq(0.8,0.9,length=100),0.7,0.0458),0)
polygon(xx, yy, col = "gray", border = NA)
I get a good graph(graph1.jpg) with the code,how can i create the second line on the graph1.jpg,to change graph1.jpg into graph2.jpg?
This is a graph1.jpg.
This is a graph2.jpg.
For the function axis() there is an argument line= that determine in which line under plot axis is drawn. Setting this argument, for example, to line=4 you can add another axis. But before plot() you should change margin setting to get more space under x axis with par(mar=...)).
par(mar=c(8,3,3,3))
x <- seq(0.5, 0.9, length = 400)
y <- dnorm(x,0.7,0.0458)
plot(x, y, type="l", yaxt="n",ann=FALSE,bty="n", xaxt="n")
#First x axis
axis(1, at=seq(0.5,0.9,by=0.1), labels=c("","",0.7, 0.8, 0.9) )
mtext("Proportions", 1, at=0.9, line=2)
#Second x axis
axis(1, line=4,at=seq(0.5,0.9,by=0.1), labels=c("","",0, 2.18, 3) )
mtext("z score", 1, at=0.9, line=6)