2015-08-25 60 views
1

我有以下相關矩陣。爲特定列繪製不同顏色矩陣的元素

 AUS AUT CAN CHE DEU EU15 FRA GBR ITA JPN USA 
AUS 1.000 0.058 0.476 0.313 0.111 0.277 0.184 0.296 0.202 0.192 0.267 
AUT 0.058 1.000 0.254 0.658 0.749 0.761 0.626 0.387 0.460 0.410 0.278 
CAN 0.476 0.254 1.000 0.390 0.321 0.534 0.377 0.538 0.391 0.231 0.746 
CHE 0.313 0.658 0.390 1.000 0.604 0.706 0.610 0.310 0.565 0.437 0.305 
DEU 0.111 0.749 0.321 0.604 1.000 0.859 0.620 0.387 0.472 0.520 0.369 
EU15 0.277 0.761 0.534 0.706 0.859 1.000 0.808 0.682 0.713 0.601 0.531 
FRA 0.184 0.626 0.377 0.610 0.620 0.808 1.000 0.467 0.553 0.444 0.357 
GBR 0.296 0.387 0.538 0.310 0.387 0.682 0.467 1.000 0.324 0.407 0.591 
ITA 0.202 0.460 0.391 0.565 0.472 0.713 0.553 0.324 1.000 0.492 0.315 
JPN 0.192 0.410 0.231 0.437 0.520 0.601 0.444 0.407 0.492 1.000 0.321 
USA 0.267 0.278 0.746 0.305 0.369 0.531 0.357 0.591 0.315 0.321 1.000 

我的目標是:將所有相關性按照升序繪製,將不同的顏色分配給美國的相關性。

我已經完成了繪製所有這樣的相關性:

x[lower.tri(x)] <- NA 
diag(x) <- NA 
x <- as.vector(gdpcor) 
x <- x[!is.na(x)] 
x <- x[order(x)] 
plot(x) 

但我無法弄清楚如何分配不同的顏色爲美國的相關性。有任何想法嗎?

回答

3

如何開始的東西,如:

x <- as.matrix(read.table(text="AUS AUT CAN CHE DEU EU15 FRA GBR ITA JPN USA 
AUS 1.000 0.058 0.476 0.313 0.111 0.277 0.184 0.296 0.202 0.192 0.267 
AUT 0.058 1.000 0.254 0.658 0.749 0.761 0.626 0.387 0.460 0.410 0.278 
CAN 0.476 0.254 1.000 0.390 0.321 0.534 0.377 0.538 0.391 0.231 0.746 
CHE 0.313 0.658 0.390 1.000 0.604 0.706 0.610 0.310 0.565 0.437 0.305 
DEU 0.111 0.749 0.321 0.604 1.000 0.859 0.620 0.387 0.472 0.520 0.369 
EU15 0.277 0.761 0.534 0.706 0.859 1.000 0.808 0.682 0.713 0.601 0.531 
FRA 0.184 0.626 0.377 0.610 0.620 0.808 1.000 0.467 0.553 0.444 0.357 
GBR 0.296 0.387 0.538 0.310 0.387 0.682 0.467 1.000 0.324 0.407 0.591 
ITA 0.202 0.460 0.391 0.565 0.472 0.713 0.553 0.324 1.000 0.492 0.315 
JPN 0.192 0.410 0.231 0.437 0.520 0.601 0.444 0.407 0.492 1.000 0.321 
USA 0.267 0.278 0.746 0.305 0.369 0.531 0.357 0.591 0.315 0.321 1.000")) 


x[lower.tri(x)] <- NA 
diag(x) <- NA 

df <- subset(as.data.frame(as.table(x), responseName = 'Corr'),!is.na(Corr)) 
df <- df[order(df$Corr),] 

ggplot(df, aes(x=1:nrow(df),y=Corr,col=Var2=='USA')) + geom_point() 

enter image description here

側面說明:如果你還沒有嘗試過,看看library(corrplot),以可視化的相關性的好方法。例如:

corrplot(x, is.corr = FALSE, method='square', diag=FALSE) 

enter image description here

+0

真是優雅!謝謝... –

+0

@ C8H10N4O2,任何想法爲什麼我得到'錯誤順序(y $ Corr):參數1不是'df < - y [順序(y $ Corr),]的向量''當執行給定塊? –

+0

@ShawnMehan是的,我從名字'y'切換到'df',但不完整。請再試一次 - 對不起 – C8H10N4O2