Related
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)
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))
Let say I want to draw this function f(x) = x^2. I want the graph of the function to be any color I want and everything else (except the axis) black. Is this possbile?
graphics.off()
par(bg = "black")
curve(x^2, -2, 2, col = "green", axes = FALSE, ann = FALSE)
title("my plot", xlab = "X", ylab = "X^2", col.lab = "white", col.main = "white")
box(col = "white")
axis(1, seq(-2, 2, 1), seq(-2, 2, 1), col = "white", col.axis = "white")
axis(2, seq(0, 4, 1), seq(0, 4, 1), col = "white", col.axis = "white")
Maybe you can ?plot to see how to make it. Below is just an example
plot(x<-seq(-5,5,0.1),
x**2,
main="square function",
ylab="f(x)=x^2",
type="l",
col=28)
I have the following code:
x <- seq(-25,25, by = .01)
plot(x, dunif(x,-10,10), type = 'l', ylab = 'probability', lwd = 2)
lines(x, dnorm(x,0,10), col = 'blue', lwd = 2)
lines(x, dstudent_t(x,4,0,10), col = 'red', lwd = 2)
lines(x, dcauchy(x,0,10), col = 'green', lwd = 2)
legend(1, 95, legend=c("Uniform (-10,10)", "Normal (0,10)", "Student t (4,0,10)", "Cauchy (0, 10)"),
col=c("black", "red", "blue", "green"), lty=1:2, cex=0.8)
Which yields me the following plot:
But the legend doesn't show up and I am not sure where the error is.
Try
legend(1, 0.05, legend=c("Uniform (-10,10)", "Normal (0,10)", "Student t (4,0,10)", "Cauchy (0, 10)"),
col=c("black", "red", "blue", "green"), lty=1:2, cex=0.8)
Your y=95 value in the legend call is way outside the y-axis range.
#jay.sf With your suggestion, the legend works! But the line it displays in the legend it sometimes dashed and sometimes not, how can i change it to a simple line for all distributions in the legend? (as for uniform and normal)
x <- seq(-25,25, by = .01)
plot(x, dunif(x,-10,10), type = 'l', ylab = 'probability', lwd = 2)
lines(x, dnorm(x,0,10), col = 'blue', lwd = 2)
lines(x, dstudent_t(x,4,0,10), col = 'red', lwd = 2)
lines(x, dcauchy(x,0,10), col = 'green', lwd = 2)
legend("topleft", legend=c("Uniform (-10,10)", "Normal (0,10)", "Student t (4,0,10)", "Cauchy (0, 10)"),
col=c("black", "red", "blue", "green"), lty=1:2, cex=0.8)
edit: worked by putting:
lty=1
One last question. It seems like i cannot just assign the plot like this, as I usually do for ggplot to save it as a PDF. What's the option here?
plot1 <- plot(x, dunif(x,-10,10), type = 'l', ylab = 'probability', lwd = 2)
lines(x, dnorm(x,0,10), col = 'blue', lwd = 2)
lines(x, dstudent_t(x,4,0,10), col = 'red', lwd = 2)
lines(x, dcauchy(x,0,10), col = 'green', lwd = 2)
legend("topleft", legend=c("Uniform (-10,10)", "Normal (0,10)", "Student t (4,0,10)", "Cauchy (0, 10)"),
col=c("black", "red", "blue", "green"), lty=1, cex=0.8)
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")