[R-br] Uso da função melt()

Michelle Bau Graczyk mbgraczyk em gmail.com
Quarta Julho 15 12:38:23 BRT 2015


Boa tarde,

Eu tenho uma matriz de correlação e eu preciso derrete-la para fazer um
mapa de calor usando a função ggplot.
Eu tentei o código abaixo mas não obtive sucesso. Alguém, por favor,
saberia me dizer como eu poderia concerta-lo?
Muito Obrigada,

Michelle

>
MatrizCorrelação<-read.table("/Users/bau/MatrizCorrelaçãoMontagem/MatrizCorrelaçãoGLOBALHoraFixa1.txt",
header=TRUE)
>
rownames(MatrizCorrelação)<-c("AA","AIG","AXP","BA","CAT","C","DD","DIS","GE","GM","HD","HON","HPQ","IBM","INTC","JNJ","JPM","KO","MCD","MMM","MO","MRK","MSFT","PFE","PG","SBC","UTX","VZ","WMT","XOM")
>
colnames(MatrizCorrelação)<-c("AA","AIG","AXP","BA","CAT","C","DD","DIS","GE","GM","HD","HON","HPQ","IBM","INTC","JNJ","JPM","KO","MCD","MMM","MO","MRK","MSFT","PFE","PG","SBC","UTX","VZ","WMT","XOM")
> show(MatrizCorrelação)
              AA         AIG         AXP           BA          CAT
   C
AA    1.00000000  0.64408120 -0.08888420  0.041492345 -0.214144744
-0.101349733
AIG   0.64408120  1.00000000 -0.10988391  0.617295058  0.088028379
-0.075095179
AXP  -0.08888420 -0.10988391  1.00000000  0.984315107 -0.116418296
 0.990267878
BA    0.04149235  0.61729506  0.98431511  1.000000000  0.209267495
 0.983175770
CAT  -0.21414474  0.08802838 -0.11641830  0.209267495  1.000000000
-0.024951121
C    -0.10134973 -0.07509518  0.99026788  0.983175770 -0.024951121
 1.000000000
DD    0.27753220 -0.06536848 -0.15801568 -0.122672004  0.176294459
-0.149479128
DIS  -0.05058204  0.07235002 -0.08595773 -0.061708245  0.096877395
 0.118710406
GE   -0.06195607  0.16175071 -0.12809978 -0.079195094  0.076001045
-0.113810967
GM   -0.17302090  0.15943410 -0.13486542 -0.125961248  0.139394559
-0.079676333
HD    0.77105494  0.27298539 -0.03724273  0.437911231  0.187631652
-0.006079136
HON  -0.10570566 -0.16445830  0.98729684  0.979601320  0.152368239
 0.990517707
HPQ   0.61508079 -0.04910831  0.89736340  0.001407106 -0.024794271
 0.879260018
IBM  -0.14325256  0.22296189 -0.09312546 -0.052157000  0.282121137
-0.098088741
INTC -0.04183314  0.17127360  0.88475464  0.977763826  0.153557706
 0.905222038
JNJ   0.39952357 -0.06097491 -0.06834544  0.016560533 -0.029220211
-0.085020474
JPM   0.01327878 -0.06526108  0.98205656  0.973315186  0.108371050
 0.975930796
KO    0.26902580  0.13846002 -0.10938672 -0.016723587 -0.035708476
-0.071168592
MCD  -0.11488201 -0.03488877  0.97909811  0.962730357  0.133474400
 0.973565190
MMM   0.85773623  0.48376538 -0.19690222  0.757310198  0.055221755
-0.203401567
MO   -0.07279634 -0.10441677  0.96450379  0.958209633  0.100724040
 0.983380621
MRK  -0.01910645  0.17189420  0.88570136  0.847176890  0.326006258
 0.936235229
MSFT  0.35282208  0.26522725  0.20144247  0.217889673  0.209585726
 0.191500107
PFE   0.71883544  0.38611556 -0.09965413  0.117492327  0.112854712
-0.078326817
PG    0.10236671  0.47079288  0.87924130  0.982927812  0.008548316
 0.887794977
SBC   0.44428023  0.22142371 -0.05977186 -0.054472383  0.120476639
-0.039427691
UTX   0.54274188  0.68774761  0.16461583  0.735488521  0.441567683
 0.086510831
VZ    0.30501108  0.24618829 -0.12762753  0.160866226  0.396779942
-0.129212617
WMT   0.77504411  0.36572997 -0.11559086  0.614663934  0.193168981
-0.109499052
XOM   0.81993320  0.29718190  0.91731073  0.742123060  0.309676262
 0.887007970
                DD         DIS           GE          GM           HD
  HON
AA    0.2775322031 -0.05058204 -0.061956065 -0.17302090  0.771054943
-0.10570566
AIG  -0.0653684788  0.07235002  0.161750706  0.15943410  0.272985391
-0.16445830
AXP  -0.1580156785 -0.08595773 -0.128099781 -0.13486542 -0.037242735
 0.98729684
BA   -0.1226720044 -0.06170825 -0.079195094 -0.12596125  0.437911231
 0.97960132
CAT   0.1762944592  0.09687739  0.076001045  0.13939456  0.187631652
 0.15236824
C    -0.1494791277  0.11871041 -0.113810967 -0.07967633 -0.006079136
 0.99051771
DD    1.0000000000  0.08783621  0.219436466  0.05454161  0.222548990
-0.09710260
DIS   0.0878362119  1.00000000 -0.097986050  0.09671508  0.019253735
 0.02492730
GE    0.2194364655 -0.09798605  1.000000000 -0.07464910  0.062579177
-0.12484867
GM    0.0545416056  0.09671508 -0.074649103  1.00000000 -0.165530144
 0.07422148
HD    0.2225489902  0.01925374  0.062579177 -0.16553014  1.000000000
-0.16409932
HON  -0.0971025994  0.02492730 -0.124848669  0.07422148 -0.164099317
 1.00000000
HPQ  -0.1167076236  0.03523964  0.259152551 -0.08918722  0.335373767
-0.06660235
IBM  -0.0983199840  0.06958259  0.092813651  0.10000633 -0.080677701
-0.06706757
INTC -0.0808370611  0.04800232 -0.052272579 -0.16632106  0.064111405
 0.96641571
JNJ   0.3049063611  0.06805802  0.009974141 -0.01028822  0.368360457
-0.05172531
JPM  -0.1374144575 -0.07747589 -0.069195345 -0.11317405  0.302011943
 0.96576594
KO   -0.0957552068 -0.08278180 -0.019693951  0.08340010  0.633619019
-0.11972446
MCD  -0.1400976129 -0.15791131 -0.076893201  0.01860467 -0.079703107
 0.97490691
MMM  -0.0229856560 -0.10135307  0.164736998 -0.13349162  0.805178951
-0.20208163
MO   -0.1497471686  0.01051476 -0.127495071  0.09013588  0.098128503
 0.95074800
MRK  -0.0679381407  0.51070153 -0.113882492  0.10834896  0.406467216
 0.90301349
MSFT  0.0754694369  0.16517350  0.106216006  0.05085640  0.361052148
 0.24126114
PFE   0.0809256468  0.13541261  0.140722998  0.01548618  0.237424055
-0.10050867
PG   -0.1388098856 -0.05085018 -0.098617228 -0.02184240  0.760744742
 0.96286144
SBC  -0.2021367506 -0.02090362 -0.014672962  0.11867336  0.539474608
-0.04806091
UTX   0.2894449830  0.02297845 -0.083819849  0.02513422  0.625110674
 0.08030012
VZ    0.0008030282 -0.02372907 -0.031313893 -0.11937086  0.338167833
-0.19417485
WMT   0.2899811736 -0.05719813  0.121443858 -0.10411422  0.762089961
-0.06754262
XOM  -0.1091177189 -0.01506886 -0.180892010 -0.18349266  0.623279211
-0.17881846
              HPQ         IBM         INTC          JNJ         JPM
  KO
AA    0.615080794 -0.14325256 -0.041833138  0.399523572  0.01327878
 0.269025800
AIG  -0.049108313  0.22296189  0.171273601 -0.060974907 -0.06526108
 0.138460020
AXP   0.897363395 -0.09312546  0.884754635 -0.068345443  0.98205656
-0.109386725
BA    0.001407106 -0.05215700  0.977763826  0.016560533  0.97331519
-0.016723587
CAT  -0.024794271  0.28212114  0.153557706 -0.029220211  0.10837105
-0.035708476
C     0.879260018 -0.09808874  0.905222038 -0.085020474  0.97593080
-0.071168592
DD   -0.116707624 -0.09831998 -0.080837061  0.304906361 -0.13741446
-0.095755207
DIS   0.035239643  0.06958259  0.048002324  0.068058023 -0.07747589
-0.082781796
GE    0.259152551  0.09281365 -0.052272579  0.009974141 -0.06919534
-0.019693951
GM   -0.089187222  0.10000633 -0.166321056 -0.010288220 -0.11317405
 0.083400099
HD    0.335373767 -0.08067770  0.064111405  0.368360457  0.30201194
 0.633619019
HON  -0.066602346 -0.06706757  0.966415706 -0.051725309  0.96576594
-0.119724456
HPQ   1.000000000 -0.15701234  0.151721547  0.107106093  0.87748415
 0.009943835
IBM  -0.157012338  1.00000000 -0.039948654  0.020173673 -0.09548061
-0.058925664
INTC  0.151721547 -0.03994865  1.000000000  0.003201919  0.88754083
-0.065405458
JNJ   0.107106093  0.02017367  0.003201919  1.000000000  0.17881168
 0.190810501
JPM   0.877484151 -0.09548061  0.887540829  0.178811675  1.00000000
-0.087498384
KO    0.009943835 -0.05892566 -0.065405458  0.190810501 -0.08749838
 1.000000000
MCD  -0.079793246 -0.08487497  0.951691306  0.012224842  0.96546727
-0.089030871
MMM   0.003599907 -0.00184816 -0.026441216  0.323647219  0.02841221
 0.697119121
MO    0.265279473 -0.08384818  0.955166076  0.345472431  0.97526394
-0.084530558
MRK  -0.065863614  0.02588809  0.810354526  0.246594467  0.85285390
-0.079041477
MSFT  0.201191442  0.17888449  0.246199067  0.189760581  0.22872513
 0.170259396
PFE   0.082804173  0.01478451  0.071951248  0.528572295  0.11845951
-0.061489407
PG    0.180362173 -0.06510797  0.948012402  0.415851593  0.88791235
-0.010360697
SBC   0.314991719  0.14796807 -0.040912484  0.659737204  0.01986489
 0.096273094
UTX  -0.044531751  0.24482269  0.474124511 -0.141775588  0.01967263
 0.320491202
VZ    0.293833023  0.28141939  0.136536306  0.746279724  0.44116583
-0.146987598
WMT  -0.164553642 -0.10344137  0.305813548 -0.185844027 -0.01222057
 0.429589230
XOM   0.873558490 -0.01355011  0.135825195  0.095164950  0.90935280
 0.023801710
              MCD          MMM          MO         MRK       MSFT
PFE
AA   -0.114882005  0.857736231 -0.07279634 -0.01910645 0.35282208
 0.71883544
AIG  -0.034888767  0.483765381 -0.10441677  0.17189420 0.26522725
 0.38611556
AXP   0.979098108 -0.196902215  0.96450379  0.88570136 0.20144247
-0.09965413
BA    0.962730357  0.757310198  0.95820963  0.84717689 0.21788967
 0.11749233
CAT   0.133474400  0.055221755  0.10072404  0.32600626 0.20958573
 0.11285471
C     0.973565190 -0.203401567  0.98338062  0.93623523 0.19150011
-0.07832682
DD   -0.140097613 -0.022985656 -0.14974717 -0.06793814 0.07546944
 0.08092565
DIS  -0.157911312 -0.101353066  0.01051476  0.51070153 0.16517350
 0.13541261
GE   -0.076893201  0.164736998 -0.12749507 -0.11388249 0.10621601
 0.14072300
GM    0.018604672 -0.133491624  0.09013588  0.10834896 0.05085640
 0.01548618
HD   -0.079703107  0.805178951  0.09812850  0.40646722 0.36105215
 0.23742405
HON   0.974906915 -0.202081627  0.95074800  0.90301349 0.24126114
-0.10050867
HPQ  -0.079793246  0.003599907  0.26527947 -0.06586361 0.20119144
 0.08280417
IBM  -0.084874971 -0.001848160 -0.08384818  0.02588809 0.17888449
 0.01478451
INTC  0.951691306 -0.026441216  0.95516608  0.81035453 0.24619907
 0.07195125
JNJ   0.012224842  0.323647219  0.34547243  0.24659447 0.18976058
 0.52857229
JPM   0.965467274  0.028412214  0.97526394  0.85285390 0.22872513
 0.11845951
KO   -0.089030871  0.697119121 -0.08453056 -0.07904148 0.17025940
-0.06148941
MCD   1.000000000 -0.002245452  0.94996006  0.79129517 0.13975360
-0.06190441
MMM  -0.002245452  1.000000000  0.12314993  0.39646696 0.46448561
 0.76680157
MO    0.949960063  0.123149928  1.00000000  0.80766662 0.16859328
 0.34554920
MRK   0.791295169  0.396466958  0.80766662  1.00000000 0.33130833
 0.48887353
MSFT  0.139753596  0.464485612  0.16859328  0.33130833 1.00000000
 0.25001730
PFE  -0.061904406  0.766801566  0.34554920  0.48887353 0.25001730
 1.00000000
PG    0.967108956  0.877382077  0.95432399  0.88725460 0.29352175
 0.51502779
SBC  -0.051205675  0.565865102  0.02652422  0.04986324 0.23363406
 0.37208816
UTX  -0.166869876  0.728133059 -0.31543056  0.17848601 0.54720408
 0.23349921
VZ   -0.114262073  0.401455663  0.34122580  0.48851378 0.32428938
 0.48834627
WMT   0.323884209  0.749895547 -0.08402821 -0.04128828 0.55588607
 0.12075279
XOM  -0.048449716  0.264692382  0.20692791  0.23032417 0.31490297
 0.11798174
               PG         SBC         UTX            VZ         WMT
XOM
AA    0.102366713  0.44428023  0.54274188  0.3050110822  0.77504411
 0.81993320
AIG   0.470792883  0.22142371  0.68774761  0.2461882948  0.36572997
 0.29718190
AXP   0.879241301 -0.05977186  0.16461583 -0.1276275273 -0.11559086
 0.91731073
BA    0.982927812 -0.05447238  0.73548852  0.1608662257  0.61466393
 0.74212306
CAT   0.008548316  0.12047664  0.44156768  0.3967799421  0.19316898
 0.30967626
C     0.887794977 -0.03942769  0.08651083 -0.1292126166 -0.10949905
 0.88700797
DD   -0.138809886 -0.20213675  0.28944498  0.0008030282  0.28998117
-0.10911772
DIS  -0.050850179 -0.02090362  0.02297845 -0.0237290706 -0.05719813
-0.01506886
GE   -0.098617228 -0.01467296 -0.08381985 -0.0313138935  0.12144386
-0.18089201
GM   -0.021842401  0.11867336  0.02513422 -0.1193708629 -0.10411422
-0.18349266
HD    0.760744742  0.53947461  0.62511067  0.3381678330  0.76208996
 0.62327921
HON   0.962861445 -0.04806091  0.08030012 -0.1941748467 -0.06754262
-0.17881846
HPQ   0.180362173  0.31499172 -0.04453175  0.2938330232 -0.16455364
 0.87355849
IBM  -0.065107969  0.14796807  0.24482269  0.2814193875 -0.10344137
-0.01355011
INTC  0.948012402 -0.04091248  0.47412451  0.1365363063  0.30581355
 0.13582519
JNJ   0.415851593  0.65973720 -0.14177559  0.7462797239 -0.18584403
 0.09516495
JPM   0.887912350  0.01986489  0.01967263  0.4411658296 -0.01222057
 0.90935280
KO   -0.010360697  0.09627309  0.32049120 -0.1469875979  0.42958923
 0.02380171
MCD   0.967108956 -0.05120567 -0.16686988 -0.1142620730  0.32388421
-0.04844972
MMM   0.877382077  0.56586510  0.72813306  0.4014556626  0.74989555
 0.26469238
MO    0.954323988  0.02652422 -0.31543056  0.3412257993 -0.08402821
 0.20692791
MRK   0.887254598  0.04986324  0.17848601  0.4885137831 -0.04128828
 0.23032417
MSFT  0.293521747  0.23363406  0.54720408  0.3242893772  0.55588607
 0.31490297
PFE   0.515027787  0.37208816  0.23349921  0.4883462710  0.12075279
 0.11798174
PG    1.000000000  0.07123866  0.55045995  0.3788365053  0.71045317
 0.31342135
SBC   0.071238658  1.00000000 -0.05513004  0.5813479300  0.04242171
 0.41568437
UTX   0.550459948 -0.05513004  1.00000000  0.0958598232  0.75842292
 0.77676007
VZ    0.378836505  0.58134793  0.09585982  1.0000000000  0.02010254
 0.51042235
WMT   0.710453169  0.04242171  0.75842292  0.0201025354  1.00000000
 0.21252902
XOM   0.313421347  0.41568437  0.77676007  0.5104223538  0.21252902
 1.00000000
>
>
> as.data.frame(as.dist(MatrizCorrelação))
Erro em as.data.frame.default(as.dist(MatrizCorrelação)) :
  cannot coerce class ""dist"" to a data.frame
>
#MatrizCorrelação<-read.table(paste0("/Users/bau/MatrizCorrelaçãoMontagem/MatrizCorrelaçãoGLOBALHoraFixa",i,".txt")))
> melted_MatrizCor<-melt(MatrizCorrelação)
Using  as id variables
>
>
> ggplot(data=melted_MatrizCor,aes(x=Var1,y=Var2,fill=value)) + geom_tile()
Erro em eval(expr, envir, enclos) : objeto 'Var1' não encontrado
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://listas.inf.ufpr.br/pipermail/r-br/attachments/20150715/ee0a533b/attachment.html>


Mais detalhes sobre a lista de discussão R-br