繁体   English   中英

R:平行坐标 Plot 没有 GGally

[英]R: Parallel Coordinates Plot without GGally

我正在使用 R 编程语言。 I am using a computer that does not have a USB port or an internet connection - I only have R with a few preloaded libraries (eg ggplot2, reshape2, dplyr, base R).

是否可以仅使用“ggplot2”库而不是“ggally”来制作“平行坐标”图(例如下面)?

#load libraries (I do not have GGally)
library(GGally)

#load data (I have MASS)
data(crabs, package = "MASS")

#make 2 different parallel coordinate plots
ggparcoord(crabs)
ggparcoord(crabs, columns = 4:8, groupColumn = "sex")

在此处输入图像描述

谢谢

资料来源: https://homepage.divms.uiowa.edu/~luke/classes/STAT4580-2020/parcor.html

实际上,您甚至不需要ggplot ,这只是标准化值的 plot(减去均值除以 SD)。 所以你可以用任何能够做到这一点的绘图 function 来实现这个逻辑:最干净和最简单的方法是在 base R 中的步骤:

在此处输入图像描述

# Standardising the variables of interest
data(crabs, package = "MASS")
crabs[, 4:8] <- apply(crabs[, 4:8], 2, scale)
# This colour solution works in great generality, although RColorBrewer has better distinct schemes
mycolours <- rainbow(length(unique(crabs$sex)), end = 0.6)
# png("gally.png", 500, 400, type = "cairo", pointsize = 14)
par(mar = c(4, 4, 0.5, 0.75))
plot(NULL, NULL, xlim = c(1, 5), ylim = range(crabs[, 4:8]) + c(-0.2, 0.2),
     bty = "n", xaxt = "n", xlab = "Variable", ylab = "Standardised value")
axis(1, 1:5, labels = colnames(crabs)[4:8])
abline(v = 1:5, col = "#00000033", lwd = 2)
abline(h = seq(-2.5, 2.5, 0.5), col = "#00000022", lty = 2)
for (i in 1:nrow(crabs)) lines(as.numeric(crabs[i, 4:8]), col = mycolours[as.numeric(crabs$sex[i])])
legend("topright", c("Female", "Male"), lwd = 2, col = mycolours, bty = "n")
# dev.off()

您可以在可以方便地绘制多条线的任何 package 中应用此逻辑(x 轴具有 integer 值,y 轴具有标准化变量线),但此解决方案没有额外的依赖关系,并且不会因为孤立的 package 有 3 个函数从 CRAN 中清除。

在没有“GGally”的情况下,我发现与此最接近的是使用“MASS”库的内置 function:

#source: https://stat.ethz.ch/R-manual/R-devel/library/MASS/html/parcoord.html
library(MASS)
parcoord(state.x77[, c(7, 4, 6, 2, 5, 3)])

ir <- rbind(iris3[,,1], iris3[,,2], iris3[,,3])
parcoord(log(ir)[, c(3, 4, 2, 1)], col = 1 + (0:149)%/%50)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM