How to set the second x-aixs in the graph? - r

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)

Related

Reduce space between names.arg and axis (box) in barplot

I created a simple barplot surrounded by a box. Is there any way to move my names closer to the box (space marked in blue)?
MWE:
set.seed(1)
count <- runif(n = 3, min = 0, max = 10)
names <- letters[seq(from = 1, to = 3)]
barplot(height = count,
names.arg = names,
horiz = TRUE,
las = 1)
box()
Here are two ways to do this. You can use rect instead of box to move the box boundary to the left:
barplot(height=count, names.arg=names, horiz=TRUE, las=1)
bounds <- par("usr")
rect(bounds[1]-.1, bounds[3], bounds[2], bounds[4], xpd=NA)
Or you can add the y-axis separately which lets you control where the labels are plotted:
x <- barplot(height=count, horiz=TRUE, las=1)
box()
axis(2, x, names, las=1, tick=FALSE, mgp=c(2, .35, 0))
Adjust the middle value in mgp to position the labels (see ?par)

set axes to meet using axis() R plot. Base R, figures for Publication

I've found an issue when setting pos = n when I use the axes command for creating charts in R. When the y-axis range is -6 to -1, it all works perfectly, but when my range is for example -5 to -2 the axes won't meet at the specified position; the y-axis omits the -5 label and tick mark.
The code for one of my charts is as follows:
x <- c(3179047.6, 1956470.4, 609295.9, 136037.6)
y <- c(-4.758698, -4.227640, -4.019197, -2.653719)
model_fit <- lm(y~x)
par(mar=c(2.1, 2.1, 2.5, 1.1)) # I want to reduce the white space around the margins
plot(x, y, axes = FALSE, xlab = NULL, ylab = NULL, # I set the axes manually afterwards
ylim = c(c(min(0, y)), ceiling(max(y))),
xlim = c(0, 1000000*ceiling(max(x)/1000000)))
lines(x4, predict(model_fit), lty = 2, col = "red")
mtext(side = 1, line = 1.1, expression(paste(epsilon^"2")), cex = 0.7) # Greek symbol squared, title repositioned, font size lowered.
mtext(side = 2, line = 0, "Y-axis Title\n(units)", cex = 0.7) # font size lowered
mtext(side = 3, line = -0.5, "Main\nTitle",
cex = 0.8, font = 2)
axis(1, seq(0, 1000000*ceiling(max(x)/1000000), 1000000),
labels = seq(0, 1000000*ceiling(max(x)/1000000), 1000000),tck = -0.02, cex.axis = 0.6, padj = -3, pos = floor(min(y4)))
axis(2, seq(floor(min(y)), ceiling(max(y)), 1),
labels = seq(floor(min(y)), ceiling(max(y)), 1), tck = -0.02, cex.axis = 0.6, padj = 2, pos = 0)
par(mar=c(5.1, 4.1, 4.1, 2.1)) # Resetting the default margin sizes
For the axis() commands I've first set the range by seq(0, 1000000*ceiling(max(x)/1000000), 1000000), and then I repeated the command by setting labels = seq(0, 1000000*ceiling(max(x)/1000000), 1000000) to manually force the code to return the tick marks and labels at the correct position.
Then for the x-axis (axis 1), I set the position where it meets the y-axis (axis 2) at the minimum value of the y axis range by setting pos = floor(min(y4). The issue is for the chart produced by the above code, instead of having the y-axis extend down to meet the x-axis, there's a space and no tick or label for the -5 point.
This only happens when the range is between -2 and -5, for my chart where the range is -1 to -6, the two axes meet.
Is there some underlying code in the axis command I need to override?

Defining limits of box around a barplot in R

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)

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")))

Y axis out of plotting region with barplot() in R

I'm having problems getting the y axis on a horizontal barplot() within the plotting region. See this example, I thought that using ylim and/or yaxp would stop this going off the plotting region, but it doesn't seem to work.
I've tried to reproduce the set up I've got:
x <- matrix(abs(rnorm(34)), nrow = 34, ncol = 3)
rownames(x) <- c(seq(0,6600,200))
barplot(x[,3], horiz=TRUE, space = 0.4, main = "Title", las=1, cex.names=0.8, ylab="y label")
But the axis goes of the plotting region if I add ylim:
barplot(x[,3], horiz=TRUE, space = 0.4, ylim = c(0,25), yaxp=c(0,25,1), main = "Title", las=1, cex.names=0.8, ylab="y label")
For some (strange?) reason, barplot has xpd = TRUE by default, setting this to false will cause it to clip like most plot functions:
barplot(x[,3], horiz=TRUE, space = 0.4, ylim = c(0,25), yaxp=c(0,25,1),
main = "Title", las = 1, cex.names = 0.8, ylab = "y label", xpd = FALSE)
The key here is to forget about ylim when using barplot and instead just send the desired plotting range in the data:
barplot(x[1:25,3], horiz=TRUE, space = 0.4, yaxp=c(0,25,1), main = "Title", las=1,
cex.names=0.8, ylab="y label")
Also note that indexing in R starts at 1 and not at 0 as it might in some other languages.

Resources