mathlete
mathlete

Reputation: 6682

How to invert the order of the y-axis labels in a levelplot?

I would like to change the labels of the y-axis ("Row") from decreasing (top to bottom) to increasing (top to bottom), to correctly match the plotted values. This should be done for any y-axis labels (automatically), not just be manually defining the values at the y-axis (in other words, how can I grab out the used labels on the y-axis, invert their order and then put them back in as labels? how would panel.levelplot() look like?). Also, this should be done without using additional packages.

library(lattice)
A <- outer(1:100, 1:100, FUN = function(x, y) (x+2*y)/300)
levelplot(t(A)[, nrow(A):1], xlab = "Column", ylab = "Row")

The idea is to visually match the structure of a matrix (diagonal going from top left to bottom right). Thanks.

Upvotes: 1

Views: 1904

Answers (1)

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162341

Just set ylim=c(100,1) or, better yet, ylim=c(100.5,0.5):

## Prepare figures with and without inverted y-axes, for easy comparison 
a <- levelplot(t(A)[, nrow(A):1], xlab = "Column", ylab = "Row")
b <- levelplot(t(A)[, nrow(A):1], xlab = "Column", ylab = "Row", 
          ylim=c(100.5, 0.5))

## Plot figs side-by-side to confirm that this works
library(gridExtra)
grid.arrange(a, b, ncol=2)

enter image description here

Upvotes: 3

Related Questions