I am using layout() to create a multiple plot figure in R and only want the y-axis to show up in the bottom plots. I therefore take them out of the loop and add them in below with the axes, but when I do this, they get cut off. Here is the code I'm using:
onlytext <- function(string){
plot(0:1, 0:1, bty='n', type='n', xaxt='n', yaxt='n', xlab='', ylab='')
text(0.5, 0.5, string, cex=1.2, font=2)
}
nf <- layout(matrix(c(0,1:14), 5, 3, byrow=TRUE), heights = c(2,5,5,5,5), widths = c(1.5,4,4))
par (mar=c(.3,.3,.3,.3))
onlytext ('Approval'); onlytext ('Vote Choice')
name_vec <- c("Strongly Approve", "Approve", "Disapprove", "Strongly Disapprove")
control_means_tab <- matrix(sample(rnorm(100), 48), ncol = 6, nrow = 4)
x <- seq(1,6)
for(i in 1:3){
onlytext(name_vec[i])
for(j in 1:2){
plot(x, control_means_tab[j,], xaxt = 'n', xlab = '', ylab = '', xaxt='n', bty = "n", ylim = c((min(c(control_means_tab, scorecard_means_tab, endorsement_means_tab))- .02),(max(c(control_means_tab, scorecard_means_tab, endorsement_means_tab)) + .02)), col = "blue", pch = 19, cex =.6)
}
}
onlytext(name_vec[4])
plot(x, control_means_tab[j,], xlab = '', ylab = '', xaxt = "n", bty = "n", ylim = c((min(c(control_means_tab, scorecard_means_tab, endorsement_means_tab))- .02),(max(c(control_means_tab, scorecard_means_tab, endorsement_means_tab)) + .02)), col = "blue", pch = 19, cex =.6)
axis(1, 1:6, LETTERS[1:6])
plot(x, control_means_tab[j,], xaxt = 'n', xlab = '', ylab = '', xaxt='n', bty = "n", ylim = c((min(c(control_means_tab, scorecard_means_tab, endorsement_means_tab))- .02),(max(c(control_means_tab, scorecard_means_tab, endorsement_means_tab)) + .02)), col = "blue", pch = 19, cex =.6)
axis(1, 1:6, LETTERS[1:6])
Any thoughts on how to get that axis back is is appreciated!
I couldn't get the ylim part of you code to work, since you didn't provide information on scorecard_means_tab or endorsement_means_tab, but I think I see what you're after, in general. Give this code a try and see if it helps.
x <- seq(1, 6)
y <- matrix(sample(rnorm(100), 48), ncol=6, nrow=8)
ylabs <- rep(c("Strongly Approve", "Approve", "Disapprove", "Strongly Disapprove"), rep(2, 4))
xlabs <- rep(c("Approval", "Vote Choice"), 4)
par(mfrow=c(4, 2), mar=c(1, 1, 1, 1), oma=c(2, 4, 2, 1))
for(i in seq(dim(y)[1])) {
if(i < 6.5) {
plot(x, y[i, ], xaxt='n', xlab='', ylab='', bty="n", col="blue", pch=19, cex=0.6)
} else {
plot(x, y[i, ], xlab='', ylab='', bty="n", col="blue", pch=19, cex=0.6)
}
if(i < 2.5) mtext(xlabs[i], side=3, line=1)
if(i %in% c(1, 3, 5, 7)) mtext(ylabs[i], side=2, line=3)
}
Related
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)
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")
I am somewhat new to R and I am trying to plot the following:
x <- c(1:13)
y <- c(19.77, 18.07, 20.55, 16.42, 18.47, 20.18, 22.91, 13.18, 18.07, 17.05, 13.45, 11.11, 22.52)
y2 <- c(26.93, 24.5, 25.73, 23.65, 20.50, 29.58, 18.8, 17.38, 12.2, 18.17, 12.55, 15.63, 26.07)
par(mar=c(5,7,5,3), pch=22, col="Black")
plot(x, y2, main="TITLE", xaxt = "n", xlab='', yaxt = "n", ylab = "", type="b", col="Blue")
par(new = TRUE)
plot(x, y, xaxt = "n", xlab='', yaxt = "n", ylab = "", type="b", col="cyan", pch=21)
range <- seq(0, 30, 1)
axis(1, at=x, labels=x)
axis(2, at=range, labels=range, las=1)
mtext(side = 1, text = "X", line = 3, col="Black")
mtext(side = 2, text = "Tiempo", line = 5, xpd=FALSE, col="Black")
# Legend.
legend(x= "bottomleft", y=0.92, legend=c("Exp1","Exp2"), col=c("cyan", "blue"), pch=c("_","_"))
It plots OK, as follows:
However, I feel that the y axis is not correlating to the real values. For example, in the data (y and y2) we can see that one of the maximum values is 26.93 but in the data, the highest value is around to 23 according to the plot.
I have been playing with the range variable but I still haven't found any way to correct this. Google has been of no help since I am not sure about how should I search for this problem. Does any of you have any idea of what I am doing wrong?
Also, I want the values to range from 0 to 30, not from 11 to 23. Maybe this is related to the problem.
Any tip would be valuable! Thanks in advance.
Staying as close as possible to your code, you could change lines #5 and #7 like this:
plot(y2 ~ x, main="TITLE", xaxt = "n", xlab='', yaxt = "n", ylab = "", type = "b", col = "Blue", ylim = c(0,30))
lines(y ~ x, xaxt = "n", xlab = '', yaxt = "n", ylab = "", type = "b", col = "cyan", pch = 21)
Use matplot:
matplot(x, cbind(y2,y), t='b', col = c("blue", "cyan"), pch = c(22, 21), lty = c(1,1), xaxt = "n", xlab="", yaxt = "n", ylab = "")
axis(1, at=x, labels=x)
axis(2, at=range, labels=range, las=1)
mtext(side = 1, text = "X", line = 3, col="Black")
mtext(side = 2, text = "Tiempo", line = 5, xpd=FALSE, col="Black")
legend(x= "bottomleft", y=0.92, legend=c("Exp1","Exp2"), col=c("cyan", "blue"), pch=c("_","_"))
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.
This is perhaps an old question, but I can't find a satisfactory answer anywhere. Say that I have a following code:
par(mfrow = c(2,1), mar = c(4,4,1,1), oma=c(2,2,2,2))
stuff <- c("ed", "bla")
cols <- c("red", "blue")
for(i in 1:length(stuff)) {
x <- rnorm(10,3,2)
y <- seq(1,10)
plot(x,y, type = "o", col = cols[i], xlab = paste("stuff about", stuff[i]))}
legend("bottomright", legend = stuff, col = cols, lwd = 1, bty = "n")
par(mfrow=c(1,1))
title(main = "ed & bla", outer = T)
mtext("This is a plot", 3, line=0.5, adj=1.0, cex=1, outer=TRUE)
How can I add the legend in the bottom margin of the plot?
Re-order the plot requests, so that you plot the legend immediately after the last plot (outside the loop) but before the next modification to the plot parameters. Note that only the "legend" command has been moved:
par(mfrow = c(2,1), mar = c(4,4,1,1), oma=c(2,2,2,2))
stuff <- c("ed", "bla")
cols <- c("red", "blue")
for(i in 1:length(stuff)) {
x <- rnorm(10,3,2)
y <- seq(1,10)
plot(x,y, type = "o", col = cols[i], xlab = paste("stuff about", stuff[i]))}
legend("bottomright", legend = stuff, col = cols, lwd = 1, bty = "n")
par(mfrow=c(1,1))
title(main = "ed & bla", outer = T)
mtext("This is a plot", 3, line=0.5, adj=1.0, cex=1, outer=TRUE)
This puts the legend into the bottom-right of the bottom plot.
but you have to set the coordinate manualy
par(mfrow = c(2,1), mar = c(4,4,1,1), oma=c(2,2,2,2))
stuff <- c("ed", "bla")
cols <- c("red", "blue")
for(i in 1:length(stuff)) {
x <- rnorm(10,3,2)
y <- seq(1,10)
plot(x,y, type = "o", col = cols[i], xlab = paste("stuff about", stuff[i]))}
legend(y=-5, x=5, legend = stuff, col = cols, lwd = 1, bty = "n", xpd=NA)