R Lattice / LatticeExtra combine Barplot with Text

2019-08-05 03:52发布

Goal: I want to create a 3D barplot (as shown below) showing value z for each combination of methods "X1"-"X4" and "Y1"-"Y4", where the labels shall correspond to the rank of the z variable.

Problem: As you can see below, the labels in the image do not match the ranking values of z (rank 1 = lowest value of z, rank 16 = highest value of z). For instance, combination "X1-Y3" should have the index 1 instead of 8, because it has the lowest z-value. Hence, how can I fix this?

Image-Output (with wrong labels): Here is the image output that I get, with the wrong labels (i.e. the labels do not correspond to the ranks of the z-axis values).

3D Barplot/Textplot from reproducible R code above

R-Code: For image above, where I overlay a textplot onto a barplot.

# load necessary libraries
library(lattice)
library(latticeExtra)

# create dataframe
df.test <- data.frame(matrix(NA, nrow=16, ncol=4))
names(df.test) <- c("x", "y", "z", "z.rank")

# fill dataframe
df.test$x <- as.factor(rep(c("X1", "X2", "X3", "X4"), times=4))
df.test$y <- as.factor(rep( paste0("Y", 1:4), each=4))
set.seed(2); df.test$z <- abs(rnorm(16, 0, 1))

# labels (rankings of z, from largest to smallest)
df.test$z.rank <- as.numeric(rank(df.test$z))


# plot 1 (bars)
p1.test <- cloud(z~x+y, data=df.test, panel.3d.cloud=panel.3dbars, 
            ylab="Y", xlab="X", zlab="Z",
            xbase=0.2, ybase=0.2, scales=list(arrows=FALSE, col="black", distance=1),
            par.settings = list(axis.line = list(col = "transparent")),
            screen = list(z = 35, x = -35, y=0),
            alpha.facet = 1.00, border = "transparent",
            zoom=1.00
); print(p1.test)

# plot 2 (labels)
p2.test <- cloud(z~x+y, data=df.test, panel.3d.cloud=panel.3dtext, 
            labels=as.character(df.test$z.rank), 
            ylab="", xlab="", zlab="",
            par.settings = list(axis.line = list(col = "transparent")),
            screen = list(z = 35, x = -35, y=0),
            alpha.facet = 1.00, border = "transparent",
            zoom=1.00, cex=0.5, pos=3, offset=1.75
); print(p2.test)

# plot both (overlay p2.test onto p1.test)
print(p1.test, more=TRUE)
print(p2.test, more=FALSE)

标签: r plot 3d lattice
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-05 04:21
library(lattice)
library(latticeExtra)
df.test <- data.frame(matrix(NA, nrow=16, ncol=4))
names(df.test) <- c("x", "y", "z", "zrank")
df.test$x <- as.factor(rep(c("X1", "X2", "X3", "X4"), times=4))
df.test$y <- as.factor(rep( paste0("Y", 1:4), each=4))
set.seed(2) 
df.test$z <- abs(rnorm(16, 0, 1))
df.test$zrank <- as.numeric(rank(df.test$z))

p1.test <- cloud(z ~ x * y, data=df.test, panel.3d.cloud=panel.3dbars, 
            ylab="Y", xlab="X", zlab="Z", 
            xbase=0.2, ybase=0.2, scales=list(arrows=FALSE, col="black", distance=1),
            par.settings = list(axis.line = list(col = "transparent")),
            screen = list(z = 35, x = -35, y=0),
            alpha.facet = 1.00, border = "transparent",
            zoom=1.00)

# A modified version of panel.3dtext
mypanel.3dtext <- function (x, y, z, labels = seq_along(x), rot.mat = diag(4), 
    distance, ...)  {
    if (all(is.na(x) | is.na(y) | is.na(z))) return()
    m <- ltransform3dto3d(rbind(x, y, z), rot.mat, distance)
    panel.text(x = m[1, ], y = m[2, ], labels = labels, ...)}

p2.test <- cloud(z ~ x * y, data=df.test, panel.3d.cloud=mypanel.3dtext, 
            labels=df.test$zrank, 
            ylab="", xlab="", zlab="", col="red",
            par.settings = list(axis.line = list(col = "transparent")),
            screen = list(z = 35, x = -35, y=0),
            alpha.facet = 1.00, border = "transparent",
            zoom=1.00, cex=0.5, pos=3, offset=1.75)

print(p1.test, more=TRUE)
print(p2.test, more=FALSE)

enter image description here

查看更多
登录 后发表回答