How can I align a photo with a plot in R? - r

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...

Related

Changing the position of axis lables closet to the plot in r

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.

How to enlarge the axis labeling of scatter 3D in R

I want to enlarge the size of the axis labeling.
par(mar=c(1,1,1,1))
scatter3D(x = Eplot_sand.mean$OM, y = Eplot_sand.mean$rho_B, z = Eplot_sand.mean$Pv.mean,
phi = 20, theta = 35, bty = "b2", type = "h",alpha = 0.8, clim =c(0,150),
ticktype = "detailed", pch = 19, cex = 1.2, clab = "Pv (kPa)",
xlab = "OM (%)",
revolutions=100,
zlab = "Pv (kPa)",
ylab = "bulk density (g cm-³)",
bg.col="black",
sphere.size=100,
xlim = c(0,5),
ylim = c(1,2),
zlim = c(0,150),
col = ramp.col(c("blue", "green","orange", "red")))
Thanks in advance!
Similarly to the limit command in your function (xlim etc), you can do a cex command for your label indicating the size of text be scaled relative to the default:
scatter3D(...,
cex.lab=par("cex.lab"))
This is also slightly documented here:
https://www.rdocumentation.org/packages/scatterplot3d/versions/0.3-41/topics/scatterplot3d

How to include subscript in axis labels of a scatter3D plot in R

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.

X Axis not showing up R

I am trying to use two different Y axis with the same X Axis and when I set both axes to false the Year wont show up
library(lubridate)
x <- dataset$Date
y <- dataset$AvgCostPerKwh
z <- dataset$ActualkWhSold
par(mar=c(5, 4, 4, 6) + 0.1)
plot(year(x),y, pch = 16, axes = FALSE, ylim = c(0.030,0.090), xlab = "", ylab = "",
type = "b", col="black", main = "Wholesale Power cost")
axis(2, ylim =(range(c(y))), col = "black", las =1)
mtext("$ per KWh", side = 2, line = 2.5)
box()
par(new = TRUE)
plot(year(x),z, pch = 15, xlab = "", ylab = "",ylim=c(5000000,45000000),
axes = FALSE, type="b", col="red")
mtext("Kwh's Sold", side=4, col="red", line=4)
axis(4, ylim=(range(c(z))), col = "red", las=1)
mtext("Year", side = 1, col="black",line=2.5)
legend("topleft", legend = c("AvgCostPerKwh", "ActualKwhSold"),
text.col = c("black", "red"),
pch=c(15,15),col=c("black", "red"))
Image 1
When I set one of the plots to true, I get overlapping values one one side, but the year on the bottom shows up.
library(lubridate)
x <- dataset$Date
y <- dataset$AvgCostPerKwh
z <- dataset$ActualkWhSold
par(mar=c(5, 4, 4, 6) + 0.1)
plot(year(x),y, pch = 16, axes = TRUE, ylim = c(0.030,0.090), xlab = "", ylab = "",
type = "b", col="black", main = "Wholesale Power cost")
axis(2, ylim =(range(c(y))), col = "black", las =1)
mtext("$ per KWh", side = 2, line = 2.5)
box()
par(new = TRUE)
plot(year(x),z, pch = 15, xlab = "", ylab = "",ylim=c(5000000,45000000),
axes = FALSE, type="b", col="red")
mtext("Kwh's Sold", side=4, col="red", line=4)
axis(4, ylim=(range(c(z))), col = "red", las=1)
mtext("Year", side = 1, col="black",line=2.5)
legend("topleft", legend = c("AvgCostPerKwh", "ActualKwhSold"),
text.col = c("black", "red"),
pch=c(15,15),col=c("black", "red"))
image 2
I am not sure, I have followed other examples on here about 2 Y axis and I can't get mine to work.
I have answered my own question for anyone interested, I forgot to add
year(as.Date(dataset$Date, format = "%m/%d/%Y"),"%Y")
at the top before adding
plot(year(x), y, pch=16, axes=FALSE, ylim=c(0.030,1), xlab="", ylab="",
type="b",col="black", main="Wholesale Power Cost")

Changing the size of the text in the axis labels of an Ecdf plot

I've constructed an Ecdf plot from the Hmisc package with the following call:
require(Hmisc)
Ecdf(latency_targ1, group = CONDITION, lty = c(1, 2, 3, 4),
lwd = 4, label.curves = list(method = 'arrow',
keys = "lines", lwd = 2), xlim = c(0,500),
subtitles = FALSE, xlab = "Latency",
ylab = "Proportion latency <= x")
I have been unable to find how to change the size of the axis labels of the plot and the default size is rather small.
Try this:
Ecdf(latency_targ1, group = CONDITION, lty = c(1, 2, 3, 4),
lwd = 4, label.curves = list(method = 'arrow',
keys = "lines", lwd = 2), xlim = c(0,500),
subtitles = FALSE, xlab = "Latency",
ylab = "Proportion latency <= x",
cex.lab=1.5, xaxt="n", yaxt="n")
axis(2, cex.axis=1.5)
axis(1, cex.axis=1.5)

Resources