R Lattice/LatticeExtra 将条形图与文本图相结合 - 标签未正确显示 [英] R Lattice / LatticeExtra combine Barplot with Textplot - Labels not properly displayed

查看:19
本文介绍了R Lattice/LatticeExtra 将条形图与文本图相结合 - 标签未正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

图像输出(标签错误):这是我得到的图像输出,标签错误(即标签不对应于 z 轴值的等级).

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

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

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)

推荐答案

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 将条形图与文本图相结合 - 标签未正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆