I want to add commas to the y-axis on my plot, but what I have tried so far is not working:
matplot(X, Y, type = "l", col = "green", xlab = "Time (years)", ylab = "Cost", main = "BLANK", ylim = (30000,60000), xlim = c(0,15))
is what I have.
Not sure how to add commas to the 30,000 - 60,000 which should appear on the y-axis.
EDIT: Sorry, what I meant in tags for matplot was this: http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/matplot.html
You could use a combination of yaxt = "n", axis and prettyNum as in
Y <- seq(30000, 60000, 2000)
X <- 0:15
matplot(X, Y, type = "l", col = "green", xlab = "Time (years)", ylab = "Cost", main = "BLANK", yaxt = "n")
axis(2, Y, labels = prettyNum(Y, big.mark = ","))
Related
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.
I want to make a dual y-axis graph that gives the absolute number change on the left hand side of total enrolment and female enrolment, and the proportion of female enrolment on the right hand side.
I've gotten the code to output the graph I want into png, but it doesn't fit within the window. I've tried expanding the size of the output file but that just enlarges the output instead of extending the window.
png("UNDERSTEM1.png", width=700, height=700)
plot(x, y1, type = "b", frame = FALSE, pch = 20,
col = "black", xlab = "Year", ylab = "", ylim=c(0,11000000))
mtext("No. of Students", side=2, line=3)
lines(x, y2, pch = 20, col = 451, type = "b", lty = 1)
par(new = TRUE)
plot(y5, type = "l", xaxt = "n", yaxt = "n",
ylab = "", xlab = "", col = "red", ylim=c(0,100), lty = 2)
axis(side=4, seq(0, 100, 10), las=2)
mtext("Percentage", side=4, line=3)
legend("topleft", legend=c("STEM", "STEM FEMALE", "PERCENTAGE FEMALE"),
col=c("black", 451, "red"), lty=c(1, 1, 2), cex=1)
dev.off()
The output of this is a graph that has the percentage values on the right-hand side like I want, but it cuts off the end of the last 0 on 100 and doesn't show the percentage label either.
I have the following code:
require(lattice)
data <- data.frame(x = c(1,1,2,3,4,5), y = c(5,1,4,6,1,7),
color = c("red", "red", "black", "blue", "yellow", "red"))
data$size <- data$x*data$y*0.2
xyplot(data$y ~ data$x, xlab = "x", ylab = "y", cex = data$size, ylim = c(0,11), xlim = c(0,6))
As you can see, this gives scatter plot with conditional sizing of circles. What I want to do is to fill the circles by condition as well (coloring provided by data$color). I tried to use fill option, but it fails to give me what I want. I will appreciate any suggestions on how to accomplish that.
We can set the pch argument to be solid circles.
library(lattice)
xyplot(data$y ~ data$x, xlab = "x", ylab = "y", cex = data$size, ylim = c(0,11), xlim = c(0,6),
col = data$color, pch = 19)
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...
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)