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.
Related
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 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 have the following code:
hc <- hclust(dist(USArrests), "ave")
plot(hc)
plot(hc, hang = 0.5, sub="", xlab ="")
Which produce the following plot:
As described in the image above. How can I push they Height label further right?
This can be done using the mgp argument to par:
par(mgp=c(2, 1, 0))
plot(hc, hang = 0.5, sub="", xlab ="")
par(mgp=c(1, 1, 0))
plot(hc, hang = 0.5, sub="", xlab ="")
The other two arguments control the tick marks and the scale position so for instance:
par(mgp=c(2, 3, 2))
plot(hc, hang = 0.5, sub="", xlab ="")
A workaround might be to remove the ylab and just add it as text at appropriate coordinates, like this:
plot(hc, hang = 0.5, sub="", xlab ="", ylab = "")
text(0, -50, "Height", srt = 90)
Maybe the use of mtext could help
plot(hc, hang = 0.5, sub="", xlab ="", ylab = "")
mtext(text = "test", side = 2, line = 2)
yielding this plot
side = 2 defines the lefthand side, and line indicates the "excentricity". You can play around some more with the line and adj parameters (and more) like this:
plot(hc, hang = 0.5, sub="", xlab ="", ylab = "")
mtext(text = "t2", side = 2, line = 2, col = "blue")
mtext(text = "t1", side = 2, line = 1, col = "blue")
mtext(text = "t0", side = 2, line = 0, col = "blue")
mtext(text = "tlow", side = 2, line = 0, col = "blue", adj = 0)
yielding the following plot
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...
I know this type of question has been posted several times, but I tried all different suggestions and nothing works for me. I would like to have the x-axis labels on my bar plot positioned at 45 degrees. My code at the moment is like this:
barplot(t(all.vac_models01[1:28,3:4]), ylab = expression("Proportion of "*italic("R"^"2")),
las=2, cex.names = 0.9,
legend.text = gsub("prop_attributed_to_visit", "Intraindividual",
gsub(pattern = "prop_attributed_to_patientID",
replacement = "Interindividual", colnames(all.vac_models01[1:28,3:4]))),
args.legend = list(x= 30, y = 1.3, xpd = T, horiz = T, bg = "white", bty ="o", box.lwd = 0))>
Try this:
x <- barplot(t(all.vac_models01[1:28,3:4]), ylab = expression("Proportion of "*italic("R"^"2")),
las=2, cex.names = 0.9,
legend.text = gsub("prop_attributed_to_visit", "Intraindividual",
gsub(pattern = "prop_attributed_to_patientID",
replacement = "Interindividual", colnames(all.vac_models01[1:28,3:4]))),
args.legend = list(x= 30, y = 1.3, xpd = T, horiz = T, bg = "white", bty ="o", box.lwd = 0),
xaxt="n")
labs <- names(t(all.vac_models01[1:28,3:4]))
text(cex=1, x=x, y=-1.25, labs, xpd=TRUE, srt=45, pos=2)
Assigning the barplot call to x allows you to store the position of the labels. Be sure to specify xaxt="n" so that the labels are not printed initially.