哈维球在R [英] Harvey balls in R
本文介绍了哈维球在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在R中创建如下的图表?
有些玩具数据看起来像这样:
#data
data <-rep(c(0,25,50,75,100),6)
data < - matrix(data,ncol = 3,byrow =数据)< - paste0(factor_,seq(3))
rownames(data)< - paste0(observation_,seq(10))
#factor_1 factor_2 factor_3
#observation_1 0 25 50
#observation_2 75 100 0
#observation_3 25 50 75
#observation_4 100 0 25
#observation_5 50 75 100
#observation_6 0 25 50
#observation_7 75 100 0
#observation_8 25 50 75
#observation_9 100 0 25
#observation_10 50 75 100
谢谢。
解决方案
这是一个快速&使用基础图形和unicode符号的脏解决方案:
library(extrafont)
#font_import()#... if您需要
loadfonts()
getPch< - function(x){
sapply(x,function(x){
switch(as.character(x),
0= - 9675,
25= - 9684,
50= - 9682,
75= - 9685,
100 =($ = $ 6
)})
}
par(mar = c(2,7,2,4))
plot(y = rep(1:nrow(data) ,ncol(data)),
x = rep(1:ncol(data),each = nrow(data)),
pch = getPch(as.vector(data)),
axes = F,xlab =,ylab =,
cex = 3,xlim = c(.5,ncol(data)+ .5),
family =Arial Unicode MS)
abline(v = 0:ncol(data)+。5)
abline(h = 1:nrow(data)+ .5)
mtext(side = 1,at = 1:ncol (data),text = colnames(data))
mtext(side = 2,at = 1:nrow(data),text = rownames(data),las = 2)
How to create a chart like the following in R?
Some toy data would look like this:
# Data
data <- rep(c(0, 25, 50, 75, 100),6)
data <- matrix(data, ncol=3, byrow=TRUE)
colnames(data) <- paste0("factor_", seq(3))
rownames(data) <- paste0("observation_", seq(10))
# factor_1 factor_2 factor_3
# observation_1 0 25 50
# observation_2 75 100 0
# observation_3 25 50 75
# observation_4 100 0 25
# observation_5 50 75 100
# observation_6 0 25 50
# observation_7 75 100 0
# observation_8 25 50 75
# observation_9 100 0 25
# observation_10 50 75 100
Thanks.
解决方案
Here's a quick & dirty solution using base graphics and unicode symbols:
library(extrafont)
# font_import() # ... if you need to
loadfonts()
getPch <- function(x) {
sapply(x, function(x) {
switch(as.character(x),
"0"=-9675,
"25"=-9684,
"50"=-9682,
"75"=-9685,
"100"=-9679
)})
}
par(mar=c(2, 7, 2, 4))
plot(y =rep(1:nrow(data), ncol(data)),
x = rep(1:ncol(data), each=nrow(data)),
pch = getPch(as.vector(data)),
axes = F, xlab = "", ylab = "",
cex = 3, xlim = c(.5, ncol(data) + .5),
family = "Arial Unicode MS")
abline(v = 0:ncol(data)+.5)
abline(h = 1:nrow(data) + .5)
mtext(side = 1, at=1:ncol(data), text=colnames(data))
mtext(side = 2, at=1:nrow(data), text=rownames(data), las=2)
这篇关于哈维球在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文