X Axis not showing up R - 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")

Related

How to rotate ylab in basic plot 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)

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

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.

Custom label on secondary y-axis in R

How can I put a custom label on the secondary y-axis in R using the following code?
library(zoo)
#--------------random data
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
z <- zoo(rnorm(5), x.Date)
z2 <- zoo(rnorm(5, sd = 0.2), x.Date)
#--------------create plot
png(filename = "Google.png", width = 480, height = 480)
plot(z, type="l", xlab="Year", lt=1, lwd=1, ylab="Google Trend")
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted",
lwd = par("lwd"), equilogs = TRUE)
par(new=TRUE)
plot(z2, type="l", ann=FALSE, lt=1, lwd=3, yaxt="n")
axis(4, ylab="Test") # Here?!
legend("topright", c("z", "z2"), lty=c(1,1),
lwd=c(1,3),col=c("black","black"),
box.col="black",bg="white")
title("Google")
dev.off()
The label "Test" does not appear in my plot...
Furthermore, how to overwrite the gridlines with the z2 and the z lines? In other words, how to bring the plotted lines to the "front"?
You need mtext for this to work:
library(zoo)
#--------------random data
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
z <- zoo(rnorm(5), x.Date)
z2 <- zoo(rnorm(5, sd = 0.2), x.Date)
#--------------create plot
#png(filename = "Google.png", width = 480, height = 480)
plot(z, type="l", xlab="Year", lt=1, lwd=1, ylab="Google Trend")
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted",
lwd = par("lwd"), equilogs = TRUE)
par(new=TRUE)
plot(z2, type="l", ann=FALSE, lt=1, lwd=3, yaxt="n")
axis(4)
#------here is the only part I added
#you specify the text, location and usually line=2 to place next to
#the y-axis labels
mtext('Test', side=4, line=2)
#-----------------------------------
legend("topright", c("z", "z2"), lty=c(1,1),
lwd=c(1,3),col=c("black","black"),
box.col="black",bg="white")
title("Google")
#dev.off()
Output:
As for z and z2 they dont seem to be behind the gridlines here.

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