R Lattice / LatticeExtra将Barplot与Textplot结合使用 - 标签显示不正确

时间:2017-09-24 18:54:42

标签: r plot 3d lattice

目标:我想创建一个3D条形图(如下所示),显示每个方法组合“X1” - “X4”和“Y1” - “Y4”的值z,其中标签应对应于z变量的等级。

问题:如下所示,图片中的标签与z的排名值不匹配(排名1 = z的最低值,排名16 = z的最高值)。例如,组合“X1-Y3”应该具有索引1而不是8,因为它具有最低的z值。因此,我该如何解决这个问题?

图像输出(标签错误):这是我得到的图像输出,标签错误(即标签与z轴值的排名不对应)。

3D Barplot/Textplot from reproducible R code above

R-Code:对于上图,我将文本图叠加到条形图上。

# 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)

1 个答案:

答案 0 :(得分:3)

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