I have the following code:
hc <- hclust(dist(USArrests), "ave")
plot(hc)
plot(hc, hang = 0.5, sub="", xlab ="")
Which produce the following plot:
As described in the image above. How can I push they Height label further right?
This can be done using the mgp argument to par:
par(mgp=c(2, 1, 0))
plot(hc, hang = 0.5, sub="", xlab ="")
par(mgp=c(1, 1, 0))
plot(hc, hang = 0.5, sub="", xlab ="")
The other two arguments control the tick marks and the scale position so for instance:
par(mgp=c(2, 3, 2))
plot(hc, hang = 0.5, sub="", xlab ="")
A workaround might be to remove the ylab and just add it as text at appropriate coordinates, like this:
plot(hc, hang = 0.5, sub="", xlab ="", ylab = "")
text(0, -50, "Height", srt = 90)
Maybe the use of mtext could help
plot(hc, hang = 0.5, sub="", xlab ="", ylab = "")
mtext(text = "test", side = 2, line = 2)
yielding this plot
side = 2 defines the lefthand side, and line indicates the "excentricity". You can play around some more with the line and adj parameters (and more) like this:
plot(hc, hang = 0.5, sub="", xlab ="", ylab = "")
mtext(text = "t2", side = 2, line = 2, col = "blue")
mtext(text = "t1", side = 2, line = 1, col = "blue")
mtext(text = "t0", side = 2, line = 0, col = "blue")
mtext(text = "tlow", side = 2, line = 0, col = "blue", adj = 0)
yielding the following plot
Related
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 am using the plot() function in r using these codes:
plot(x= T38$Variables,
y= T38$Q5_E_38, type = "b", pch=16, col = "red",
xlab = "", ylab = "", yaxt = "n",
xaxt= "n", main="RMSE ")
title(xlab = "Variables", line =1.5, cex.lab = 1.5)
title(ylab = "RMSE" , line =1.5, cex.lab = 1.5 )
axis(2, at= T38$Q5_E_38[37], col="red", cex.axis=0.6)
axis(1, cex.axis=0.6)
My problem is that the axis labels are too distant from the axis. I need to bring them closer to the axis. Please see the following picture. How Can I bring the numbers closer to the axis in Plot()?
Taking your x-axis for example:
axis(side = 1, labels = FALSE)
mtext(axTicks(1), side = 1, line = 0.5, at = axTicks(1), cex = 0.6)
Adjust line = ? as you see fit.
I want to make a scatter 3D Plot with PC_1 and PC_3 (subscripts) as labels of the x- and z-axis. I would usually do it with expression(PC[1]) but it doesn't work. I just can't find a way to solve the problem.
library(plot3D)
par(mar = c(4, 0, 3.2, 2))
scatter3D(pc$PC1, pc$PC2, pc$PC3, phi = phi, theta = 180,
colvar = as.integer(sz$t)/60, clab = c("Zeit in", "Minuten"),
col = ramp.col(c("deepskyblue", "blue", "darkorchid", "violetred", "red")),
bty = "b2", type = "b", ticktype = "detailed",
pch = 20, lwd = 2, cex.lab = 1.4, cex.axis = 1.4, cex = 1,
colkey = FALSE, main = "",
xlab = expression(PC[1]), ylab = "", zlab = expression(PC[3]),
xlim = c(min(pc), max(pc)), ylim = c(min(pc), max(pc)), zlim = c(min(pc), max(pc))
)
It just shows PC[1] and PC[3] as axis labels. PC["1"] and "PC"["1"] or any other combination also don't work.
I want to plot in R several panels with data measured along a sediment core (see image below). Next to the plots, I want to add a photo of that sediment core, aligned to the exact y coordinates as the plots (to be able to identify if a certain layer in the photo corresponds to a peak in some element in the plot).
The piece of code below produces de multipanel plot plus the photo (see below as well). As you can see using the tape measure in the photo, the y coordinates of the plots do not match the coordinates of the photo.
op <- par(mfrow = c(1,4), oma = c(5,4,2,0) + 0.1, mar = c(0,0,4,1) + 0.1, pch = 20)
plot(y = -curian3_scores$Sample, x = curian3_scores$RC1, xaxt = "n", type = "o", pch =16, ylab = "", xlab = "", lwd = 1.5, col = "blue", ylim = c(-1197.744, -40), cex.axis = 1.2)
axis(3, cex.axis = 1.2)
mtext(side = 3, text = var.PC1, line = 2.2, cex = 0.9)
mtext(side = 2, text = "Depth (mm)", line = 3, cex = 0.9)
plot(y = -curian3_scores$Sample, x = curian3_scores$RC2, xaxt = "n", yaxt = "n", type = "o", ylab = "", xlab = "", lwd = 1.5, col = "red", ylim = c(-1197.744, -40), cex.axis = 1.2)
axis(3, cex.axis = 1.2)
mtext(side = 3, text = var.PC2, line = 2.2, cex = 0.9)
plot(y = -curian3_scores$Sample, x = curian3_scores$RC3, xaxt = "n", yaxt = "n", type = "o", ylab = "", xlab = "", lwd = 1.5, col = "darkgreen", ylim = c(-1197.744, -40), cex.axis = 1.2)
axis(3, cex.axis = 1.2)
mtext(side = 3, text = var.PC3, line = 2.2, cex = 0.9)
# Charge the image as an R object with the "JPEG" package
library(jpeg)
my_image=readJPEG("~/Documents/FEINA/POSTDOCTORAT/RESILCOAST/ARTICLES/Carbon_persistence/Data_mining/CoreScanner/Core_Images_for_R/CURIAN3a_8.jpg")
# Set up a plot area with no plot
plot(y = -curian3_scores$Sample, x = curian3_scores$RC3, type = "n", axes = F, ylab = "", xlab = "")
# Get the plot information so the image will fill the plot box, and draw it
lim <- par()
rasterImage(my_image, xleft = lim$usr[1], ybottom = lim$usr[3], xright = lim$usr[2], ytop = lim$usr[4])
par(op)
Any ideas to make the photo's and plots' coordinates to match? If the ylim in each plot worked, I think this would be it... however, I think they're overruled for some reason...
I know this type of question has been posted several times, but I tried all different suggestions and nothing works for me. I would like to have the x-axis labels on my bar plot positioned at 45 degrees. My code at the moment is like this:
barplot(t(all.vac_models01[1:28,3:4]), ylab = expression("Proportion of "*italic("R"^"2")),
las=2, cex.names = 0.9,
legend.text = gsub("prop_attributed_to_visit", "Intraindividual",
gsub(pattern = "prop_attributed_to_patientID",
replacement = "Interindividual", colnames(all.vac_models01[1:28,3:4]))),
args.legend = list(x= 30, y = 1.3, xpd = T, horiz = T, bg = "white", bty ="o", box.lwd = 0))>
Try this:
x <- barplot(t(all.vac_models01[1:28,3:4]), ylab = expression("Proportion of "*italic("R"^"2")),
las=2, cex.names = 0.9,
legend.text = gsub("prop_attributed_to_visit", "Intraindividual",
gsub(pattern = "prop_attributed_to_patientID",
replacement = "Interindividual", colnames(all.vac_models01[1:28,3:4]))),
args.legend = list(x= 30, y = 1.3, xpd = T, horiz = T, bg = "white", bty ="o", box.lwd = 0),
xaxt="n")
labs <- names(t(all.vac_models01[1:28,3:4]))
text(cex=1, x=x, y=-1.25, labs, xpd=TRUE, srt=45, pos=2)
Assigning the barplot call to x allows you to store the position of the labels. Be sure to specify xaxt="n" so that the labels are not printed initially.