R:不使用GGally的平行坐标图 [英] R: Parallel Coordinates Plot without GGally

查看:43
本文介绍了R:不使用GGally的平行坐标图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.我使用的计算机没有USB端口或互联网连接-我的R仅带有一些预加载的库(例如ggplot2,reshape2,dplyr和base R).

是否有可能使平行坐标"成为可能.仅使用"ggplot2"图来绘制(例如以下)图.库而不是笨拙地"?

  #load库(我没有GGally)图书馆(GGally)#加载数据(我有MASS)数据(螃蟹,包裹="MASS")#制作2个不同的平行坐标图ggparcoord(蟹)ggparcoord(crabs,columns = 4:8,groupColumn ="sex") 

谢谢

来源:

 #标准化感兴趣的变量数据(螃蟹,包裹="MASS")螃蟹[,4:8]<-适用(螃蟹[,4:8],2,比例)#尽管RColorBrewer具有更好的独特方案,但此颜色解决方案的通用性很高mycolours<-rainbow(length(unique(crabs $ sex)),end = 0.6)#png("gally.png",500、400,类型="cairo",点数= 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 =变量",ylab =标准值")轴(1,1:5,标签= colnames(螃蟹)[4:8])abline(v = 1:5,col =#00000033",lwd = 2)abline(h = seq(-2.5,2.5,0.5),col =#00000022",lty = 2)对于(i在1:nrow(crabs))行中(as.numeric(crabs [i,4:8]),col = mycolours [as.numeric(crabs $ sex [i])])图例("topright",c("Female","Male"),lwd = 2,col = mycolours,bty ="n")#dev.off() 

您可以在可以方便地绘制多条线(如时间序列)的任何程序包中应用此逻辑(x轴为整数值,y轴为标准变量线),但是此解决方案没有额外的依赖关系,并且不会变得不可用由于从CRAN中清除了具有3个功能的孤立软件包.

I am using the R programming language. 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 (e.g. ggplot2, reshape2, dplyr, base R).

Is it possible to make "parallel coordinate" plots (e.g. below) using only the "ggplot2" library and not "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")

Thanks

Source: https://homepage.divms.uiowa.edu/~luke/classes/STAT4580-2020/parcor.html

解决方案

In fact, you do not even need ggplot! This is just a plot of standardised values (minus mean divided by SD), so you can implement this logic with any plotting function capable of doing so. The cleanest and easiest way to do it is in steps in 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()

You can apply this logic (x axis with integer values, y axis with standardised variable lines) in any package that can conveniently draw multiple lines (as in time series), but this solution has no extra dependencies an will not become unavailable due to an orphaned package with 3 functions getting purged from CRAN.

这篇关于R:不使用GGally的平行坐标图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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