Basically I only want to draw the x, y, z axis with empty plot, but with x, y, z of my own labels on it. Is it possible to do it in R? I know how to draw it in 2d plot.
Here is your answer:
library(scatterplot3d)
scatterplot3d(0,0,0, pch="", xlab="X", ylab="Y",zlab="Z",
xlim=c(0,1), ylim=c(0,1), zlim=c(0,1))
The position of Y label seems a bit strange. This is a limitation of this package. So you may set ylab="" and then manually put the label to your desired place by text(x, y, "Y")
Related
In R you can use this to remove the axis values:
x <- 1:20
y <- runif(20)
plot(x, y, axes=FALSE, frame.plot=TRUE)
Axis(side=1, labels=FALSE)
Axis(side=2, labels=FALSE)
Output:
As mentioned in this answer, you can remove the axis values and tick marks like this:
import Pkg
Pkg.add("Plots")
using Plots
# generating vectors
# x-axis
x = 1:10
# y-axis
y = rand(10)
# simple plotting
plot(x, y, ticks = false)
Output:
Is it possible to only remove the values of the axis while keeping the tick marks like in the R example?
Try
plot(x, y, formatter=(_...) -> "") #or:
plot(x, y, formatter=Returns(""))
formatter (and xformatter, yformatter) specifies a function which formats the axis labels.
I have eliminated labels on the y axis because only the relative amount is really important.
w <- c(34170,24911,20323,14290,9605,7803,7113,6031,5140,4469)
plot(1:length(w), w, type="b", xlab="Number of clusters",
ylab="Within-cluster variance",
main="K=5 eliminates most of the within-cluster variance",
cex.main=1.5,
cex.lab=1.2,
font.main=20,
yaxt='n',lab=c(length(w),5,7), # no ticks on y axis, all ticks on x
family="Calibri Light")
However, suppressing those tick labels leaves a lot of white space between the y axis label ("Within-cluster variance") and the y axis. Is there a way to nudge it back over? If I somehow set the (invisible) tick labels to go inside the axis, would the axis label settles along the axis?
Try setting ylab="" in your plot call and use title to set the label of the y-axis manually. Using line you could adjust the position of the label, e.g.:
plot(1:length(w), w, type="b", xlab="Number of clusters", ylab="",
main="K=5 eliminates most of the within-cluster variance",
cex.main=1.5,
cex.lab=1.2,
font.main=20,
yaxt='n',lab=c(length(w),5,7), # no ticks on y axis, all ticks on x
family="Calibri Light")
title(ylab="Within-cluster variance", line=0, cex.lab=1.2, family="Calibri Light")
Please read ?title for more details.
Adjust mgp, see ?par
title(ylab="Within-cluster variance", mgp=c(1,1,0), family="Calibri Light",cex.lab=1.2)
I'm beginner with plotting in 3D in R and I need help. I try to plot some easy paraboloid
library(rgl)
x <- seq(-1,1, 0.2)
y <- x
f <- function(x,y){
-(x^2+y^2)
}
z <- outer(x,y, "f")
persp3d(x, y, z, col="gray")
So, my questions are:
Can I draw only grid, or make color transparent to see also the part of "at the back"?
How to add points to the plot (on the surface, e.g to draw in other color point (1,1,2))?
See ?material3d for information on surface properties. Most of these properties, such as alpha or front="line" or back="line", can be passed directly to persp3d(). Add points with points3d() (or spheres3d()).
persp3d(x, y, z, col="gray", alpha=0.5)
points3d(1,1,2,col="red")
persp3d(x, y, z, col="gray", front="line", back="line")
spheres3d(1,1,2,col="red",radius=5) ## appropriate radius: I used x <- y <- 1:20
I'm generating numerous plots with xlim and ylim values that I'm calculating on a per-plot basis. I want to put my legend outside the plot area (just above the box around the actual plot), but I can't figure out how to get the maximum y-value of the box around my plot area.
Is there a method for even doing this? I can move the legend where I want it by manually changing the legend() x and y values, but this takes a LONG time for the amount of graphs I'm creating.
Thanks!
-JM
Here's a basic example illustrating what I think you're looking for using one of the code examples from ?legend.
#Construct some data and start the plot
x <- 0:64/64
y <- sin(3*pi*x)
plot(x, y, type="l", col="blue")
points(x, y, pch=21, bg="white")
#Grab the plotting region dimensions
rng <- par("usr")
#Call your legend with plot = FALSE to get its dimensions
lg <- legend(rng[1],rng[2], "sin(c x)", pch=21,
pt.bg="white", lty=1, col = "blue",plot = FALSE)
#Once you have the dimensions in lg, use them to adjust
# the legend position
#Note the use of xpd = NA to allow plotting outside plotting region
legend(rng[1],rng[4] + lg$rect$h, "sin(c x)", pch=21,
pt.bg="white", lty=1, col = "blue",plot = TRUE, xpd = NA)
The command par('usr') will return the coordinates of the bounding box, but you can also use the grconvertX and grconvertY functions. A simple example:
plot(1:10)
par(xpd=NA)
legend(par('usr')[1], par('usr')[4], yjust=0, legend='anything', pch=1)
legend( grconvertX(1, from='npc'), grconvertY(1, from='npc'), yjust=0,
xjust=1, legend='something', lty=1)
The oma, omd, and omi arguments of par() control boundaries and margins of plots - they can be queried using par()$omd (etc). and set (if needed) using par(oma=c()) (where the vector can have up to 4 values - see ?par)
I would like to make a plot with 4 axes in R so that it looks similar to this plot:
I've looked at the Quick-R website for advice and modified one of their examples (called A Silly Axis Example):
# specify the data
x <- c(1:5); y <- x/2;
w <- c(2:4)
z <- c(1:5)
# create extra margin room on the right for an axis
par(mar=c(5, 4, 4, 8) + 0.1)
# plot x vs. y
plot(x, y,type="b", pch=21, col="red",
yaxt="n", lty=3, xlab="", ylab="")
# add x vs. 1/x
lines(x, z, type="b", pch=22, col="blue", lty=2)
# draw an axis on the left
axis(2, at=x,labels=x, col.axis="red", las=2)
# draw an axis on the right, with smaller text and ticks
axis(4, at=w,labels=round(w,digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
# draw an axis on the top
axis(3, at=z,labels=round(z,digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
# add a title for the right axis
mtext("L", side=3, line=3, cex.lab=1,las=2, col="blue")
# add a title for the right axis
mtext("OSR", side=4, line=3, cex.lab=1,las=2, col="red")
# add a main title and bottom and left axis labels
title("", xlab="GSI", ylab="FSI")
This code produces the following plot:
I'm having difficulty figuring out how different axes can have different scales. For example, I want the top axis L, to go from 5 - 13, but if I set z <-c(5:13) it will not set the axis to these values. However, I can overwrite what the labels are:
axis(3, at=z,labels=round(c(9:13),digits=2), col.axis="blue",
las=2, cex.axis=0.7, tck=-.01)
but then if I want to plot a point with these four parameters, the point will not show up in the correct place. How should I do this?
One (perhaps cumbersome) option would be to write conversion functions that transform values between your two scales. Assuming you know the data ranges for both the top and bottom axes ahead of time, you could write a function like this:
convertScaleToBottom <- function(x,botRange,topRange){
temp <- (x - topRange[1]) / (topRange[2] - topRange[1])
return(botRange[1] + (temp * (botRange[2] - botRange[1])))
}
that takes a set of values, x, in the top axis scale and converts them to the bottom axis scale. Then you can plot the converted values and retain the originals as the labels:
z1 <- 5:13
z1Adj <- convertScaleToBottom(z1,range(x),range(z1))
# draw an axis on the top
axis(3, at=z1Adj,labels=round(z1,digits=2),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
This method is easily modified to reverse the order of the top axis:
convertScaleToBottomRev <- function(x,botRange,topRange){
temp <- (x - topRange[1]) / (topRange[2] - topRange[1])
return(botRange[2] - (temp * (botRange[2] - botRange[1])))
}