How to rotate ylab in basic plot R - r

I've created three plots, I want to set the ylab text on the top and rotate text in ylab = "text", but I can't do it using text, or par("usr") and draw a new axis. How to do it?
steps1 <- c(77:82)
steps2 <- c(72:82)
steps3 <- c(62:82)
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,3))
par(las = 1)
plot(steps1, dfL1_F[77:82],
type= "o",pch=16, lty=1, col="black", xlab = 'Step finals', ylab = 'Max F, A.U.')
abline(h=c(0.000450), lwd=1.5, lty=2, col="red")
grid(nx = NULL, ny = NULL,lty = 2, col = "gray", lwd = 1)
plot(steps2, dfL1_F[72:82],
type= "o",pch=16, lty=1, col="black", xlab = 'Step finals', ylab = 'Max F, A.U.')
grid(nx = NULL, ny = NULL,lty = 2, col = "gray", lwd = 1)
abline(h=c(0.000450), lwd=1.5, lty=2, col="red")
plot(steps3, dfL1_F[62:82],
type= "o",pch=16, lty=1, col="black", xlab = 'Step finals', ylab = 'Max F, A.U.')
grid(nx = NULL, ny = NULL,lty = 2, col = "gray", lwd = 1)
abline(h=c(0.000450), lwd=1.5, lty=2, col="red")
par(opar)
Sorry, for trouble, this is input data:
c(0.090803, 0.053571, 0.059279, 0.081256, 0.898884, 0.046667, 0.036066,
0.024925, 0.109469, 0.030897, 0.052733, 0.013766, 0.012801,
0.012609, 0.009315, 0.015449, 0.031633, 0.009842, 0.014112, 0.016614,
0.006702, 0.006865, 0.007912, 0.023806, 0.023373, 0.009024, 0.008952,
0.042152, 0.010782, 0.006892, 0.006798, 0.006797, 0.006586, 0.020231,
0.040914, 0.007164, 0.008807, 0.004308, 0.009495, 0.008776, 0.006001,
0.004247, 0.052963, 0.006756, 0.02555, 0.002508, 0.003779, 0.00453,
0.003253, 0.006861, 0.009106, 0.001776, 0.004892, 0.005216, 0.010059,
0.0045, 0.003041, 0.005896, 0.00287, 0.003333, 0.008464, 0.003295,
0.002963, 0.001889, 0.0021, 0.001508, 0.002127, 0.001287, 0.001515,
0.001391, 0.002729, 0.002551, 0.001971, 0.001635, 0.000612, 0.00097,
0.000504, 3e-04, 0.000385, 0.000322, 0.000111, 0.000111)

You can remove the ylab and create an adjustable ylab using mtext Using mtext you can move the title left and right using adj, move the title up and down using padj, and change the size of the font using cex. You may have to play with padj and adj a bit to get the label in the right position for your needs
steps1 <- c(77:82)
steps2 <- c(72:82)
steps3 <- c(62:82)
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,3))
par(las = 1)
plot(steps1, dfL1_F[77:82],
type= "o",pch=16, lty=1, col="black", xlab = 'Step finals', ylab = '')
abline(h=c(0.000450), lwd=1.5, lty=2, col="red")
#adj will move the title left and right and padj moves the title up and down
mtext("MAX F", col = "black", adj=-0.4, padj=-1, cex=0.6)
grid(nx = NULL, ny = NULL,lty = 2, col = "gray", lwd = 1)
plot(steps2, dfL1_F[72:82],
type= "o",pch=16, lty=1, col="black", xlab = 'Step finals', ylab = '')
#adj will move the title left and right and padj moves the title up and down
mtext("MAX F.", col = "black", adj=-0.4, padj=-1, cex=0.6)
grid(nx = NULL, ny = NULL,lty = 2, col = "gray", lwd = 1)
abline(h=c(0.000450), lwd=1.5, lty=2, col="red")
plot(steps3, dfL1_F[62:82],
type= "o",pch=16, lty=1, col="black", xlab = 'Step finals', ylab = '')
#adj will move the title left and right and padj moves the title up and down
mtext("MAX F", col = "black", adj=-0.4, padj=-1, cex=0.6)
grid(nx = NULL, ny = NULL,lty = 2, col = "gray", lwd = 1)
abline(h=c(0.000450), lwd=1.5, lty=2, col="red")
par(opar)

Related

how to plot this in R

how would I go about plotting this in R?
Here is the code I have already tried but it doesn't work!
x <- seq(-5, 5, length=200)
y <- dnorm(x, mean= -1, sd=1)
t = sqrt(0.5)
z <- seq(-5, 5, length=200)
a <- dnorm(z, mean=1, sd=t)
plot(z, a, type="l", lwd=2, col='red', xlim = c(-5,5), ylim = c(0,0.65), xlab = "Observed Value", ylab = "Probability Density")
lines(x,y, col='blue', lwd=2)
abline(v = 0, col = "darkgreen")
Modify your call to plot as follows:
plot(
z,
a,
type = "l",
lwd = 2,
col = 'red',
xlim = c(-5, 5),
ylim = c(0, 0.65),
xlab = "Observed Value",
ylab = "Probability Density",
main = expression(paste(pi[1], "= .3, ", pi[2], "= .7"))
)
lines(x, y, col = 'blue', lwd = 2)
abline(v = 0, col = "darkgreen", lty=2, lwd = 1.5)

For a graph with 2 y axis in R, how do I add the label to the second y-axis?

Goal: I'm trying to make a graph showing the water levels for a region over time that has two y axis (the first being the total megaliters of water in the water supply and the second being the dam capacity).
Problem: The title for my second y-axis isn't formatted correctly: it overlaps the increments on the y-axis and to the left of the values. I want to move it to the right.
Here's my code:
#data
years <- c(2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021)
levels <- c(646137, 450429, 279954, 190300, 191843, 411849, 481370, 626907)
percents <- c(71.9, 50.1, 31.2, 21.2, 21.4, 45.9, 53.6, 69.8)
allData <- data.frame(years, levels, percents)
levelsOverTime <- data.frame(years, levels)
percentsOverTime <- data.frame(years, percents)
#plot
plot(percentsOverTime, type="l", ylab="Water levels as percentages of dam capacity", main="Water Levels Over Time", xlab="Year", col="blue", ylim=c(0, 100))
par(new = TRUE)
plot(levelsOverTime, type="l", xaxt="n", yaxt="n", ylab="", xlab="", col="red", ylim=c(0, 650000))
axis(side = 4)
mtext("Total water stored in 6 major dams (megalitres)", side=4)
Try this way. You may change location of text using line = .
par(mar = c(5, 4, 4, 6) + 0.1)
plot(percentsOverTime, type="l",
main="Water Levels Over Time", xlab="", ylab = "", col="blue", ylim=c(0, 100), axes = FALSE)
axis(2, ylim = c(0,100), col = "blue", las = 1)
mtext("Water levels as percentages of dam capacity", side = 2, line = 2.5, col = "blue")
box()
par(new = TRUE)
plot(levelsOverTime, type="l", xaxt="n", yaxt="n", ylab="", xlab="", col="red", ylim=c(0, 650000),
axes = FALSE)
mtext("Total water stored in 6 major dams (megalitres)", side = 4, col = "red", line = 4)
axis(4, ylim = c(0, 650000), col = "red", las = 1)
axis(1, years)
mtext("Year", side = 1, col = "black", line = 2.5)
legend("bottomleft", legend = c("precents", "levels"),
text.col = c("blue", "red"), col = c("black", "red"), lty = c(1,1))
par(mar = c(5, 4, 4, 6) + 0.1)
plot(percentsOverTime, type="l",
main="Water Levels Over Time", xlab="", ylab = "", col="blue", ylim=c(0, 100), axes = FALSE)
axis(2, ylim = c(0,100), col = "blue", las = 1)
mtext("Water levels as percentages of dam capacity", side = 2, line = 2.5, col = "blue")
box()
par(new = TRUE)
plot(levelsOverTime, type="l", xaxt="n", yaxt="n", ylab="", xlab="", col="red", ylim=c(0, 650000),
axes = FALSE)
mtext("Total water stored in 6 major dams (megalitres)", side = 4, col = "red", line = 4)
ytick <- seq(0, 650000, by = 100000)
axis(4, ylim = c(0, 650000), col = "red", las = 1, labels = format(ytick,scientific = FALSE), at = ytick)
axis(1, years)
mtext("Year", side = 1, col = "black", line = 2.5)
legend("bottomleft", legend = c("precents", "levels"),
text.col = c("blue", "red"), col = c("black", "red"), lty = c(1,1))

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

How can I align a photo with a plot in 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...

R - Base Line Chart - How to remove X and Y axes lines

I am trying to get rid of the X and Y axes lines in R but it seems impossible. I am running the below code:
par(mar=c(5,5,5,5), cex.main = 1.5, col.main = "gray30", bg="white", cex =
0.8, family="Calibri", las=1)
plot(y1~x, col=blk_blue, type="l", lwd=2, ylim = y1_limits, xlab = x_label,
ylab = y1_label, las=1, axes=FALSE)
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col =
"gray90", border=NA)
grid(nx = 5, ny = 5, col = "white", lty = "solid", lwd = 1)
grid(nx = 10, ny = 10, col = "white", lty = "dotted", lwd = 1)
par(new=TRUE, las=1, bty="n")
plot(y1~x, col=blk_blue, type="l", lwd=2, ylim = y1_limits, xlab = x_label,
ylab = y1_label)
axis(side=1, col = "white", labels = FALSE)
axis(side=2, col = "grey90", labels = FALSE, ylim = y1_limits)
axis(side=3, col = "grey90", labels = FALSE, tick = FALSE)
mtext(y2_label, side=4, line=3)
par(new=TRUE, bty="n", las=1, bty="n")
plot(y2~x, col=blk_red, type="l", lwd = 2, axes=FALSE, xlab = "", ylab="",
ylim=y2_limits, las=1, xaxt="n")
axis(side = 4, ylim = y2_limits, col="grey90")
and the resulting chart looks like the below:
As you can see there are still some remainder black segments in the x-axis.
How can I remove those?
In your calls to plot(...), set axes=FALSE
In your calls to axis(...), set labels=TRUE
I believe this solves the issue, it prevents the black axis lines from being plotted in the first place but it still plots the numbers along the axes.

Resources