R Lattice / LatticeExtra combine Barplot with Text

2019-09-26 11:59发布

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

问题:正如你可以看到下面,图像中的标签不匹配z的分级值(等级1 =最低的z值,排名16 =最高的Z值)。 例如,组合“X1-Y3”应具有索引1,而不是如图8所示,因为它具有最低的z值。 因此,我该如何解决这一问题?

图像输出(有错误的标签):这里是图像输出,我得到的,用错误的标签(即标签不对应于z轴值的行列)。

R-代码:对于图像的上方,其中I覆盖一个textplot到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)

Answer 1:

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)



文章来源: R Lattice / LatticeExtra combine Barplot with Textplot - Labels not properly displayed
标签: r plot 3d lattice