I have this problem. I got a heatmap, (but i suppose this applies to every plot) but I need to mirror my y-axis.
I got here some example code:
library(gstat)
x <- seq(1,50,length=50)
y <- seq(1,50,length=50)
z <- rnorm(1000)
df <- data.frame(x=x,y=y,z=z)
image(df,col=heat.colors(256))
This will generate the following heatmap
But I need the y-axis mirrored. Starting with 0 on the top and 50 on the bottom. Does anybody has a clue as to what I must do to change this?
See the help page for ?plot.default, which specifies
xlim: the x limits (x1, x2) of the plot. Note that ‘x1 > x2’ is
allowed and leads to a ‘reversed axis’.
library(gstat)
x <- seq(1,50,length=50)
y <- seq(1,50,length=50)
z <- rnorm(1000)
df <- data.frame(x=x,y=y,z=z)
So
image(df,col=heat.colors(256), ylim = rev(range(y)))
Does this work for you (it's a bit of a hack, though)?
df2<-df
df2$y<-50-df2$y #reverse oredr
image(df2,col=heat.colors(256),yaxt="n") #avoid y axis
axis(2, at=c(0,10,20,30,40,50), labels=c(50,40,30,20,10,0)) #draw y axis manually
The revaxis function in the plotrix package "reverses the sense of either or both the ‘x’ and ‘y’ axes". It doesn't solve your problem (Nick's solution is the correct one) but can be useful when you need to plot a scatterplot with reversed axes.
I would use rev like so:
df <- data.frame(x=x,y=rev(y),z=z)
In case you were not aware, notice that df is actually a function. You might want to be careful when overwriting. If you rm(df), things will go back to normal.
Don't forget to relabel the y axis as Nick suggests.
For the vertical axis increasing in the downward direction, I provided two ways (two different answers) for the following question:
R - image of a pixel matrix?
Related
When I plot a PCA and then the corresponding biplot, the axis are not always in the same direction, just like in these pictures:
These are the functions, I used:
(pc <- prcomp(dat5, center=T, retx=T, scale=T)); summary(pc)
plot(pc$x[,1:2], pch=""); text(pc$x[,1:2], cex=.5, labels=(row.names(dat5)), col=as.numeric(dat$ObCl))
biplot(princomp(dat5, cor=T), cex=.5)
How can I change the axis direction of one of those, to make them the same?
The sign of PCs being arbitrary, you can change it/them by multiplying one or more PCs by -1. Note that this stands only for representation only, depending on what you do, eg if you use $rotation you may also need to change the corresponding columns as well. An example with iris follows. Hope this helps.
p <- prcomp(iris[, -5])
plot(p$x[, 1:2], asp=1, xlab="PC1", ylab="PC2")
plot(cbind(p$x[, 1], p$x[, 2]*-1), asp=1, xlab="PC1", ylab="PC2")
I have a dataframe in which values c are specified for Cartesian coordinates (x, y). I use plot() and the result has too much space in Y-axis. Is there a way to edit that?
library(raster)
x <- c(1:60)
y <- c(2:5)
c <- rnorm(60)
Data1 <- data.frame(x, y, c)
raster1 <- rasterFromXYZ(Data1)
plot(raster1)
While not a fix, it helps to note that in plot(), xlim and ylim end up providing the max and min.
Obviously, it's handled differently in the raster library. When I use plot(raster1, ylim=c(2,5)), the axis isn't adjusted at all. However, if you use plot(raster1, xlim=c(0,20)) clearly affects the x-axis.
I'm not sure why it won't affect the y-axis.
I'm using the rgl package in r to plot some data. As done here:
http://www.r-bloggers.com/creating-3d-geographical-plots-in-r-using-rgl/
For some reason the scale does not align with the graph.
I changed the scale of the X and Z axis to increase the relief, which I initially thought was causing the issue, but in the example below, even if I change 0.02 to 1 the issue occurs.
library(rgl)
rdata <- c(0.8926,0.8986,0.9478,0.9672,0.916,0.912,0.9324,0.9532,0.9488,0.9376,0.921,0.927,0.9728,0.956,0.9318,0.9202)
labs <-c(100,200,500,1000)
rmatrix <- matrix(rdata, nrow=4,ncol=4,)
dimnames(rmatrix) <- list(labs,labs)
y <- as.matrix(rmatrix)
x <- 0.02*(1:nrow(y))
z <- 0.02*(1:ncol(y))
rgl.surface(x, z, y, color="red", back="lines")
axis3d('x--', labels=row.names(rmatrix),color="black")
Why is this happening?
Thanks for your help!
Mat
Without supplying a value to the labels argument in axis3d, I get an axis with six tick marks. Since you supply a vector with only four values to the labels argument, it looks like axis3d recycles those values to cover all the tick marks.
Tell axis3d at what data values you'd like to place the tick marks by supplying a value to the at argument:
axis3d('x--', at = x, labels=row.names(rmatrix),color="black")
p.s. I had to add the following line before rgl.surface() to avoid getting a segfault
rgl.open()
I am trying to construct a graph consisting of 2-3 filled.contour plots next to each other. The color scale is the same for all plots, and I would like only one z value key plot. I am having difficulties to do this with par(mfrow=c(1,3))
Example code:
x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
filled.contour(x,y,z)
filled.contour(x,y,z,color.palette=rainbow)
z2 <- z
z2[5,5] <- Inf
filled.contour(x,y,z2,col=rainbow(200),nlevels=200)
Is it possible to stack 2-3 of these plots next to each other with only one z value color key? I can do this in GIMP but I was wondering if it is natively in R possible.
No I do not think this is possible in filled.contour.
Although extensions have been written for you already. To be found here, here and here and a legend code here.
[If you are using the filled.contour3 function referred to on those sites, and using a more recent version, then you need to use the upgrade fix referred to in this SO post].
Using those codes I produced:
It can be solved with cowplot.
The clue is the cowplot::draw_plot function which successfully represent the plot (cowplot::draw_grob(~filled.contour(x,y,z))).
Later the grob representations can be binded.
The possible problem is that each plot will have own legend.
x <- 1:5
y <- 1:5
z <- matrix(outer(x,y,"+"),nrow=5)
z2 <- z
z2[5,5] <- Inf
cowplot::plot_grid(~filled.contour(x,y,z), ~filled.contour(x,y,z,color.palette=rainbow), ~filled.contour(x,y,z2,col=rainbow(200),nlevels=200))
Created on 2023-02-14 with reprex v2.0.2
The following code creates 3 vectors, and displays them as interlaced histograms:
a <- c(1,2,3)
b <- c(1,1,2)
c <- c(1,1,1)
l <- list(a,b,c)
multhist(l, col=c("red","green","blue"),xlim=c(0,5))
However, when I specify this xlim=c(0,5), I would expect this to set the x axis range, but it does not seem to do so. The x-axis appears to only range between about 1.0 and 1.4. Is there a different way to specify the x-axis range for a multhist?
Maybe not perfect, but a start:
edit: remove x-axis labels, add box
multhist(l, col=c("red","green","blue"),
breaks=seq(0,5,by=0.2),names.arg=rep("",25))
box(bty="l") ## add box around bottom and left edges
multhist is a bit of a hack (I know, I wrote it!) -- it uses barplot internally, so the x axis is indexing the positions of the bars rather than the actual values.
See also
http://www.math.mcmaster.ca/bolker/R/misc/multhist.pdf
http://www.math.mcmaster.ca/bolker/R/misc/multhist.Rnw
for some other ideas about how to display binned data from multiple groups.