I am trying to design a fairly customized plot in R.
One thing I want to do is add tick marks that are different from the labels - that is, only every 5th tick mark will be labeled. I didn't see an easy way to do this, so I did this:
plot(x = Freq_, y = Mean_ipsi,
pch = 20,
ylim = c(-0.5, .9),
col = 1 + (ProbF < .05) + (ProbF < .01),
xaxt = 'n',
xlab = "Frequency (MHz)", ylab = "z-in minus z-out",
main = "Temporal, Engle 1, Epi, subjectwise",
yaxt = 'n')
mtext(text = seq(1.56, 35.1, by = 1.95),
side = 1, at = seq(1.56, 35.1, by = 1.95), cex = .5,line = 0.25)
axis(1, at = Freq_, tick = TRUE, labels = NA)
and it worked just as I wanted.
But when I changed some of the code preceding mtext I got unexpected results
plot(x = Freq_, y = Mean_ipsi,
pch = 20,
ylim = c(-0.5, .9),
col = "red",
xlab = "Frequency (MHz)", ylab = "z-in minus z-out",
main = "Temporal, Engle 1, Epi, subjectwise
\n p values for difference between ipsi and contra",
yaxt = 'n', type = 'o')
mtext(text = seq(1.56, 35.1, by = 1.95),
side = 1, at = seq(1.56, 35.1, by = 1.95),
cex = .5,line = 0.25)
axis(1, at = Freq_, tick = TRUE, labels = NA)
Now, in addition to the x-axis being labeled with every numbers 1.56, 3.51, etc. I get large numbers (cex = 1, I think) at 5, 10 and so on. I don't want these.
I have no idea what is happening here.
You're missing xaxt="n" in your second version.
Freq_ <- seq(1.56, 35.1, by = 1.95)
Mean_ipsi <- (0.01 * Freq_)
ProbF <- 0.0
#First Version
plot(x = Freq_, y = Mean_ipsi,
pch = 20,
ylim = c(-0.5, .9),
col = 1 + (ProbF < .05) + (ProbF < .01),
xaxt = 'n',
xlab = "Frequency (MHz)", ylab = "z-in minus z-out",
main = "Temporal, Engle 1, Epi, subjectwise",
yaxt = 'n')
mtext(text = seq(1.56, 35.1, by = 1.95),
side = 1, at = seq(1.56, 35.1, by = 1.95), cex = .5,line = 0.25)
axis(1, at = Freq_, tick = TRUE, labels = NA)
#=============================================
#Second Version
plot(x = Freq_, y = Mean_ipsi,
pch = 20,
ylim = c(-0.5, .9),
col = "red",
xlab = "Frequency (MHz)", ylab = "z-in minus z-out",
main = "Temporal, Engle 1, Epi, subjectwise
\n p values for difference between ipsi and contra",
yaxt = 'n', type = 'o') ##### <----- add xaxt="n" here #####
mtext(text = seq(1.56, 35.1, by = 1.95),
side = 1, at = seq(1.56, 35.1, by = 1.95),
cex = .5,line = 0.25)
axis(1, at = Freq_, tick = TRUE, labels = NA)
Related
I have a 2 x 2 figure where the columns represent two different variables and the rows represent two different locations. How can I add the names of the two locations to the two rows?
Example Data
library(biwavelet)
par(mfrow = c(2,2),
oma = c(3,3,0,0) + 0.1,
mar = c(1,1,1,1) + 0.1)
dat <- as.data.frame(matrix(nrow = 500, ncol = 2))
dat[1] <- seq(1,500)
dat[2] <- sin(dat[1])
# top-left figure
plot(wt(dat),
xaxt = 'n',
xlab = "",
cex.axis = 1.5,
cex.lab = 1.5)
axis(1, at = seq(0, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
title("Variable 1", line = 0.1, cex.main = 1.5)
# top-right figure
dat[2] <- sin(dat[1]*.5)
plot(wt(dat),
xaxt = 'n',
col.axis = 'NA',
xlab = "",
ylab = "")
axis(1, at = seq(0, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
title("Variable 2", line = 0.1, cex.main = 1.5)
# bottom-left figure
dat[2] <- sin(dat[1]*.25)
plot(wt(dat),
cex.axis = 1.5)
# bottom-right figure
dat[2] <- sin(dat[1]*.125)
plot(wt(dat),
col.axis = 'NA',
ylab = "",
xlab = "")
axis(1, at = seq(0, 500, by = 100), cex.axis = 1.5)
title(xlab = "Time (hours)",
ylab = "Period",
outer = TRUE,
line = 1.5,
cex.lab = 1.5)
The ideal figure would look like this
Neither of the suggested solutions so far have put the right-hand labels in the orientation requested. You cannot do so with mtext, but rather need to use text, first allowing text to display outside the plot region with par(xpd=NA). (See ?text and ?par, where one reads that the srt,rotation, parameter only applies to text):
par(mfrow=c(2,2), xpd=FALSE)
par(mar = c(1,1,1,1) + 2)
plot(disp ~ mpg, data = mtcars)
mtext("disp 1", side=3)
plot(I(2*disp) ~ mpg, data = mtcars)
mtext("disp 2", side=3); par(xpd=NA)
text("mpg 1", x=36, y=500, srt=270)
plot(disp ~ I(2*mpg), data = mtcars); par(xpd=NA)
plot(I(2*disp) ~ I(2*mpg), data = mtcars)
text("mpg 2", x=72, y=500, srt=270)
It is not as automatic as mtext in the sense that one needs to look at each plotting figure separately to derive a estimate for the x and y positions in the plotting coordinates.
Here's one method:
par(mfrow=c(2,2))
par(mar = c(1,1,1,1) + 0.1)
plot(disp ~ mpg, data = mtcars)
mtext("disp 1", side=3)
plot(I(2*disp) ~ mpg, data = mtcars)
mtext("disp 2", side=3)
mtext("mpg 1", side=4)
plot(disp ~ I(2*mpg), data = mtcars)
plot(I(2*disp) ~ I(2*mpg), data = mtcars)
mtext("mpg 2", side=4)
Unfortunately, mtext does not support rotating text, so you're stuck with the right labels being oriented as they are.
In case anyone stumbles across this page, the code I ended up using was based on #IRTFM answer, see below. It was challenging finding the appropriate y = in the text() function. I used an iterative approach and found the y = to be much lower than I anticipated.
library(biwavelet)
par(mfrow = c(2,2), xpd=F)
par(mar = c(1,1,1.1,1.1) + 0.1,
oma = c(3,3,0.5,0.5) + 0.1)
dat <- as.data.frame(matrix(nrow = 500, ncol = 2))
dat[1] <- seq(1,500)
dat[2] <- sin(dat[1])
# top-left figure
plot(wt(dat),
xaxt = 'n',
xlab = "",
cex.axis = 1.5,
cex.lab = 1.5)
axis(1, at = seq(0, 500, by = 100), cex.axis = 1.5, col.axis = 'NA')
mtext("Variable 1", side = 3, cex = 1.5, line = 0.1)
box(lty = "solid", col = 'black')
# top-right figure
dat[2] <- sin(dat[1]*.5)
plot(wt(dat),
xaxt = 'n',
col.axis = 'NA',
xlab = "",
ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
mtext("Variable 2", side = 3, cex = 1.5, line = 0.1)
text("Location 1", x = 520, y = 4.1, srt = 270, cex = 1.5, xpd=NA)
box(lty = "solid", col = 'black')
# bottom-left figure
dat[2] <- sin(dat[1]*.25)
plot(wt(dat),
cex.axis = 1.5,
xlab = "",
ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
box(lty = "solid", col = 'black')
# bottom-right figure
dat[2] <- sin(dat[1]*.125)
plot(wt(dat),
col.axis = 'NA',
ylab = "",
xlab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
text("Location 2", x = 520, y = 4.5, srt = 270, cex = 1.5, xpd=NA)
box(lty = "solid", col = 'black')
title(xlab = "Time (hours)",
ylab = "Period",
outer = TRUE,
line = 1.5,
cex.lab = 1.5)
I am making a 4-panel figure where each column represents a different variable and each row represents a different location. When I add the first two plots (i.e., the top row) there is a box surrounding each plot. However, when I add the bottom two figures (i.e., the bottom row) the box around the top row plots disappears. Any idea how to make sure there is a box around each plot?
Example
Make the two figures of the top row first
library(biwavelet)
par(mfrow = c(2,2), xpd=FALSE)
par(mar = c(1,1,1.1,1.1) + 0.1,
oma = c(3,3,0.5,0.5) + 0.1)
dat <- as.data.frame(matrix(nrow = 500, ncol = 2))
dat[1] <- seq(1,500)
dat[2] <- sin(dat[1])
# top-left figure
plot(wt(dat),
xaxt = 'n',
xlab = "",
cex.axis = 1.5,
cex.lab = 1.5)
axis(1, at = seq(0, 500, by = 100), cex.axis = 1.5, col.axis = 'NA')
mtext("Variable 1", side = 3, cex = 1.5, line = 0.1)
box(lty = "solid", col = 'black')
# top-right figure
dat[2] <- sin(dat[1]*.5)
plot(wt(dat),
xaxt = 'n',
col.axis = 'NA',
xlab = "",
ylab = ""); par(xpd = NA)
axis(1, at = seq(100, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
mtext("Variable 2", side = 3, cex = 1.5, line = 0.1); par(xpd = NA)
text("Location 1", x = 520, y = 4.1, srt = 270, cex = 1.5)
box(lty = "solid", col = 'black')
Now add bottom two plots and watch the black box around the top two plots disappear
# bottom-left figure
dat[2] <- sin(dat[1]*.25)
plot(wt(dat),
cex.axis = 1.5,
xlab = "",
ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
box(lty = "solid", col = 'black')
# bottom-right figure
dat[2] <- sin(dat[1]*.125)
plot(wt(dat),
col.axis = 'NA',
ylab = "",
xlab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5); par(xpd = NA)
text("Location 2", x = 520, y = 4.5, srt = 270, cex = 1.5)
box(lty = "solid", col = 'black')
title(xlab = "Time (hours)",
ylab = "Period",
outer = TRUE,
line = 1.5,
cex.lab = 1.5)
I have tried using box(lty = "solid", col = 'black'), which has not worked. I have also tried using fg = 'black' and bty = "o" inside of plot(), which has not worked.
How can I keep the black box around the top two plots?
With par(xpd=NA) before your text() commands you are doing the xpd=NA setting globally, hence influencing all subsequent plotting, and your bottom row graphs are somehow overdrawing the top row ones. Just move the xpd=NA inside your text() functions (e.g. text('Location 1', ..., xpd=NA)) and everything should work as expected:
library(biwavelet)
par(mfrow = c(2,2), xpd=F)
par(mar = c(1,1,1.1,1.1) + 0.1,
oma = c(3,3,0.5,0.5) + 0.1)
dat <- as.data.frame(matrix(nrow = 500, ncol = 2))
dat[1] <- seq(1,500)
dat[2] <- sin(dat[1])
# top-left figure
plot(wt(dat),
xaxt = 'n',
xlab = "",
cex.axis = 1.5,
cex.lab = 1.5)
axis(1, at = seq(0, 500, by = 100), cex.axis = 1.5, col.axis = 'NA')
mtext("Variable 1", side = 3, cex = 1.5, line = 0.1)
box(lty = "solid", col = 'black')
# top-right figure
dat[2] <- sin(dat[1]*.5)
plot(wt(dat),
xaxt = 'n',
col.axis = 'NA',
xlab = "",
ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
mtext("Variable 2", side = 3, cex = 1.5, line = 0.1)
text("Location 1", x = 520, y = 4.1, srt = 270, cex = 1.5, xpd=NA)
box(lty = "solid", col = 'black')
# bottom-left figure
dat[2] <- sin(dat[1]*.25)
plot(wt(dat),
cex.axis = 1.5,
xlab = "",
ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
box(lty = "solid", col = 'black')
# bottom-right figure
dat[2] <- sin(dat[1]*.125)
plot(wt(dat),
col.axis = 'NA',
ylab = "",
xlab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
text("Location 2", x = 520, y = 4.5, srt = 270, cex = 1.5, xpd=NA)
box(lty = "solid", col = 'black')
title(xlab = "Time (hours)",
ylab = "Period",
outer = TRUE,
line = 1.5,
cex.lab = 1.5)
I want to display two plots with the same x-values above each other. But the plots don't align.
How can I align them?
Code:
dat <- data.frame(d = LETTERS[1:5], c = c(39, 371, 389, 378, 790), r = c(39,
332, 18, -11, 412))
par(mfrow=c(2,1))
plot(dat$c, type = "s", ylim = c(0, max(dat$c)), xlab = "", ylab = "", axes = FALSE, col = "#4572a7", lwd = 2)
axis(1, at = c(1:length(dat$c)), labels = dat$d, lty = 0)
axis(2, lty = 0, las = 1)
barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE)
axis(2, lty = 0, las = 1)
abline(h = 0, col = "#bbbbbb")
We need to get the x-coordinates of the center of each bar and use those coordinates as the x-values of the first plot. We also need to set the same xlim values for each plot:
# Get x coordinates of center of each bar
pr = barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE,
plot=FALSE)
par(mfrow=c(2,1))
# Apply the x coordinates we just calculated to both graphs and give both
# graphs the same xlim values
plot(pr, dat$c, type = "s", ylim = c(0, max(dat$c)), xlab = "", ylab = "", axes = FALSE,
col = "#4572a7", lwd = 2, xlim=range(pr) + c(-0.5,0.5))
axis(1, at = pr, labels = dat$d, lty = 0)
axis(2, lty = 0, las = 1)
barplot(dat$r, names.arg = dat$d, col = "#008000", border = NA, axes = FALSE,
xlim=range(pr) + c(-0.5,0.5))
axis(2, lty = 0, las = 1)
In R, is there a way to add means to a plot at specific x-axis. For example, I want to do something like:
plot(1, 1, xlim = c(0, 6.5), ylim = c(0,300), type = 'n', xlab = '', ylab = '', xaxt = 'n') #xaxs="i",yaxs="i")
boxplot(dataset,at = 0.5, add = T, range = 6,yaxt = 'n')
points(mean(dataset), at = 0.5, add = T)
I get a message saying "at" and "add" are 'not a graphical parameter'. Is there a workaround?
So I am using RStudio. There are six different values (data_a, data_b, data_c, data_d, data_e, and data_f) each with 11 numbers. My current code looks like follows:
par(xpd = FALSE)
par(mar=c(8,4.5,2,1))
plot(1, 1, xlim = c(0, 6.5), ylim = c(0,300), type = 'n', xlab = '', ylab = '', xaxt = 'n') #xaxs="i",yaxs="i")
boxplot(data_a,at = 0.5, add = T, range = 6,yaxt = 'n')
boxplot(data_b,at = 1.5, add = T, range = 6,yaxt = 'n')
boxplot(data_c,at = 2.5, add = T, range = 6,yaxt = 'n')
boxplot(data_d,at = 4, add = T, range = 6,yaxt = 'n')
boxplot(data_e,at = 5, add = T, range = 6,yaxt = 'n')
boxplot(data_f,at = 6, add = T, range = 6,yaxt = 'n')
axis(2, at = 150, pos = -0.65, tck = 0, labels = 'X axis label',cex.axis=1.1)
axis(1, at = c(0.5,1.5,2.5,4,5,6),labels=c('','','','','',''))
axis(1, at = c(1.5,5),pos= -25,labels=c('label 1','labe 2'),tick=FALSE)
axis(1, at = c(3.25),labels=c(''),tck=-0.15)
axis(1, at = c(3.25),pos = -50,labels=c('Y axis label'),tick=FALSE)
abline(v=3.25)
par(xpd = NA)
text(0.5,-30, expression("a))
text(1.5,-30, expression("b"))
text(2.5,-30,"c")
text(4,-30, expression(d))
text(5,-30, expression("e"))
text(6,-30,"f")
Now I want to be able to add mean.
You can try this to get the desired plot (with randomly generated data_a, data_b etc.):
par(xpd = FALSE)
par(mar=c(8,4.5,2,1))
plot(1, 1, xlim = c(0, 6.5), ylim = c(0,300), type = 'n', xlab = '', ylab = '', xaxt = 'n') #xaxs="i",yaxs="i")
data_a <- runif(11, 0, 300)
data_b <- runif(11, 0, 300)
data_c <- runif(11, 0, 300)
data_d <- runif(11, 0, 300)
data_e <- runif(11, 0, 300)
data_f <- runif(11, 0, 300)
boxplot(data_a,at = 0.5, add = T, range = 6,yaxt = 'n')
boxplot(data_b,at = 1.5, add = T, range = 6,yaxt = 'n')
boxplot(data_c,at = 2.5, add = T, range = 6,yaxt = 'n')
boxplot(data_d,at = 4, add = T, range = 6,yaxt = 'n')
boxplot(data_e,at = 5, add = T, range = 6,yaxt = 'n')
boxplot(data_f,at = 6, add = T, range = 6,yaxt = 'n')
axis(2, at = 150, pos = -0.65, tck = 0, labels = 'X axis label',cex.axis=1.1)
axis(1, at = c(0.5,1.5,2.5,4,5,6),labels=c('','','','','',''))
axis(1, at = c(1.5,5),pos= -25,labels=c('label 1','label 2'),tick=FALSE)
axis(1, at = c(3.25),labels=c(''),tck=-0.15)
axis(1, at = c(3.25),pos = -50,labels=c('Y axis label'),tick=FALSE)
abline(v=3.25)
par(xpd = NA)
text(0.5,-30, expression("a"))
text(1.5,-30, expression("b"))
text(2.5,-30,"c")
text(4,-30, expression("d"))
text(5,-30, expression("e"))
text(6,-30,"f")
points(c(0.5,1.5,2.5,4,5,6), c(mean(data_a), mean(data_b), mean(data_c), mean(data_d), mean(data_e), mean(data_f)), pch = 22, col = "darkgrey", lwd = 7)
With boxplots, the location of the plots on the x axis are at the units (1, 2, 3, etc.). You can check this using the function locator().
Here is an example to add means as a red circle to a boxplot using the iris dataset:
boxplot(Sepal.Length ~ Species, data = iris)
points(seq_along(levels(iris$Species)), with(iris, tapply(Sepal.Length, Species, mean)), col = "red")
Does anyone know how to extract the 'y' off the y-axis while preserving the variable names in the following plot:
par(mar = c(5,7,4,2) +.01)
matrix <- matrix(rnorm(100) ,ncol = 2, nrow =6)
y <- 1:6
par(mar = c(5,7,4,2) +.01)
plot(matrix[,1], y, cex = .8, pch = 20, xlab = "Standardized Mean Differences", col = "darkblue", main = "Balance Assessment", yaxt = "n")
points(matrix[,2], y, cex = .8, pch = 20, col ="cyan")
abline(v = 0, col = "gray50", lty =2)
text(y =1:6, par("usr")[1], labels = c("Var1", "Var2", "Var3", "Var4", "Var5", "Var6"), pos = 2, xpd = TRUE, srt = 0, cex = .8, font = 1, col = "blue")
It's minor, but it's driving me crazy. Thanks!
Just set ylab='' to remove it.