changing a Barplot to GGplot

Please ask your questions about R Markdown here.

Hello!

Currently I have a barplot that looks as the following

The code looks as the following; you can ignore the histogram coding
library(corrplot)
cor=data.frame(c)
hist(cor$y_High[-c(1,2,3)], col="orange")
cor2=cor[abs(cor$y_High)>.2325,]
nm=rownames(cor2)[-c(1,2,3)]
correlations=abs(cor2[,1])[-c(1,2,3)]
barplot(correlations)

I am currently trying to 1. make the barplot be in ascending order 2. format the barplot with the proper title, x and y axis heading. I am aware that the ggplot function is available to do everything I am asking for; however i having issues when trying to run the ggplot

the following error shows up if I replace barplot with ggplot:
Error: data must be a data frame, or other object coercible by fortify(), not a numeric vector

What do i need to do in order to fix the error OR is it possible to make all those changes I mentioned before using just barplot

Thanks for your help!

Hi @mdg202,

You can't just replace barplot() with ggplot(). In order to use ggplot2 plots you need to first specify the data you want to plot with ggplot() then you need to sequentially add layers to the plot (composed with the + symbol). Also, ggplot() accepts data frames to plot, so you can't just feed in a vector of numbers.

I didn't have your data, so I use the iris data set. The first block of code is getting the correlation data in the proper structure for plotting with ggplot, then I make the bar plot.

library(dplyr)
library(corrr)
library(forcats)

data <- iris %>% 
  select(-Species) %>% 
  correlate() %>% 
  shave() %>% 
  stretch() %>% 
  drop_na() %>% 
  mutate(
    xvar = glue::glue('{x} vs. {y}'),
    xvar = fct_reorder(xvar, r) # reorder bars in ascending order
    )

# Now we can make the plot, you can customize the appearance
ggplot(data, aes(x = xvar, y = r)) +
  geom_col() +
  labs(
    y = 'Correlation coefficient',
    x = '',
    title = 'Here is my awesome title'
  ) +
  coord_flip() +
  theme(legend.position = 'none')

Hey Matt.

so my data is as follows for cor

however, I just want to use the top 10 variables, which I already got through the nm formula.

what do you recommend to get the correlation to the proper structure?

If you can share part of the data in a way that I can copy-and-paste it, I would be happy to help. If you take your data and run dput(data), you can copy and paste the output here and enclose it in three back ticks (found below the escape key), like this:

```
# enter code here
```
structure(list(y_High = c(1, 0.52284462381307, 0.769237804490072, 
-0.27345653762845, 0.310765738936609, 0.242204371067535, 0.356047857951224, 
0.270915591372287, 0.304149438083611, 0.250990741135676, 0.270511342803479, 
0.235546136830897, 0.234221042704035), y_Low = c(0.52284462381307, 
1, 0.823416504713659, 0.149400322238551, -0.217358915523106, 
-0.146996665672384, -0.26554301126859, -0.232046645231559, -0.210387497808702, 
-0.242548589975152, -0.216064781374194, -0.145005330176965, -0.171130416710285
), y_Today = c(0.769237804490072, 0.823416504713659, 1, -0.0357228749863642, 
0.0387324388824313, 0.0481455717480241, 0.024862730940692, -0.00816849692259382, 
0.028259127181329, -0.0215494421400653, 0.0109568583727757, 0.0239417271422657, 
-0.00059954020618912), Lag1 = c(-0.173091472980147, 0.091533262960949, 
-0.0205911644702689, 0.727910014185484, 0.0238009551809319, -0.00558936312264367, 
-0.224855628169016, -0.188014227189969, -0.186048540676573, -0.186152588592026, 
-0.18747275707083, -0.0585150625082766, -0.293458391782183), 
   Lag2 = c(-0.163998081290555, 0.0758365175809571, -0.0335916558457053, 
   0.53182264102381, -0.0544763727487788, -0.0245141309425425, 
   -0.257395488258827, -0.201816797357297, -0.20471695376941, 
   -0.217236603229293, -0.218545561336701, -0.0797350506621262, 
   -0.232766285127939), Lag3 = c(-0.0906194197349866, 0.076918545109277, 
   0.0154431818824328, 0.33281261824954, -0.131941898814527, 
   -0.0301440358255991, -0.232448082457241, -0.170864694277005, 
   -0.178699333496651, -0.188116139861762, -0.161238607000944, 
   -0.0886989094612622, -0.1751209917605), Lag4 = c(-0.117140855029656, 
   0.0643028742404273, -0.0418729964401144, 0.169839869443857, 
   -0.179689880863982, -0.0422802202591513, -0.166561698586465, 
   -0.0960494465258455, -0.138859281109135, -0.114340585146404, 
   -0.129625694621504, -0.0873132530562577, -0.103060221046529
   ), Lag5 = c(-0.113755192354647, 0.0162020878699251, -0.0366445128986775, 
   -0.0424874460298926, -0.248659115314046, -0.0527181954035197, 
   -0.17701928099649, -0.118379341617645, -0.0957692595601706, 
   -0.147779467477273, -0.108934610683249, -0.0922983458519567, 
   -0.102527466902897), P1 = c(-0.0224248829500832, -0.0376354778566962, 
   -0.0393890280720117, -0.0570166343796589, -0.0167202556677217, 
   -0.0324622652108995, 0.17121234852312, 0.153899285524911, 
   0.143860297022976, 0.1150096355225, 0.155134429816379, -0.0191857198165952, 
   0.114299580141159), P2 = c(0.0315177341387727, -0.0768363282796056, 
   -0.0458684987075317, -0.215367718512824, -0.00578761168555371, 
   -0.034824982517886, 0.264334253125177, 0.25153892750009, 
   0.248836609220222, 0.21267049841631, 0.243623924829571, -0.000369403954100229, 
   0.226690335667394), P3 = c(-0.27345653762845, 0.149400322238551, 
   -0.0357228749863642, 1, -0.0855789093494021, -0.030588688426995, 
   -0.421159912962481, -0.328542935819366, -0.339598372038162, 
   -0.345925583724904, -0.340849994214403, -0.13067540904444, 
   -0.428609440054892), P4 = c(0.310765738936609, -0.217358915523106, 
   0.0387324388824313, -0.0855789093494021, 1, 0.459803821587225, 
   0.674043692942179, 0.479809781761018, 0.500349507703541, 
   0.463204298201201, 0.450284175224949, 0.472749553258322, 
   0.383491037280305), P5 = c(0.158080154233715, -0.135358416187869, 
   0.0204798463854085, 0.0090160089457687, 0.36942351931181, 
   0.485930094757103, 0.319085420311691, 0.257443492079277, 
   0.275541416650495, 0.228342160649479, 0.201828227275591, 
   0.172012050614661, 0.0776092884805625), P6 = c(0.148483745421061, 
   -0.115507306321236, 0.0294667691460887, 0.0976098255979519, 
   0.300996627200495, 0.739497335383913, 0.287003374964331, 
   0.233170297542836, 0.195456180343806, 0.266465877178675, 
   0.140651044736174, 0.360179686094341, 0.0410017895645625), 
   P7 = c(0.0261179572912586, -0.0434271462846339, -0.000691164575920728, 
   0.0467186328822411, 0.0597810565841721, 0.282305723062678, 
   0.0742433089058216, 0.09671722790712, 0.00687370895596902, 
   0.108551279902697, -0.0131689753002156, 0.0670030372190484, 
   0.0422700807915693), P8 = c(0.242204371067535, -0.146996665672384, 
   0.0481455717480241, -0.030588688426995, 0.459803821587225, 
   1, 0.423136829176774, 0.348094770691552, 0.319966750037057, 
   0.379805515433103, 0.260961836399518, 0.647808076662489, 
   0.157500505133063), P9 = c(0.121120180946172, -0.0926189421061565, 
   0.0370414996007255, 0.032068041653293, 0.268192765269019, 
   0.458238472908308, 0.229543714368674, 0.141494598674766, 
   0.191449773023116, 0.204751101130031, 0.112982964256361, 
   0.136231994128269, 0.0390321390000964), P10 = c(0.356047857951224, 
   -0.26554301126859, 0.024862730940692, -0.421159912962481, 
   0.674043692942179, 0.423136829176774, 1, 0.761746325373771, 
   0.769011405166628, 0.743850374453961, 0.738218044554685, 
   0.428700920236045, 0.661110022922387), FXI.Lag1 = c(-0.0885995595730363, 
   0.0757831696169481, 0.0281625961534119, 0.438443489501388, 
   -0.00839873123014617, -0.00728608195050165, -0.164082624257242, 
   -0.147547824605133, -0.139420917849874, -0.127481042556776, 
   -0.14785895837932, -0.0582199712089322, -0.223831205291118
   ), FXI.Lag2 = c(-0.111147274291972, 0.0638842385322862, -0.00834252676801908, 
   0.349999400318423, -0.0608237400019766, -0.0212043400564077, 
   -0.164271786289531, -0.153556123911412, -0.128041556381816, 
   -0.152228450787114, -0.151516303081093, -0.0710961227323968, 
   -0.15328312109163), FXI.Lag3 = c(-0.106281318249597, 0.00655078570666858, 
   -0.0365985330036599, 0.235970006625769, -0.110911453459551, 
   -0.030116047402043, -0.171619935061499, -0.152517258076085, 
   -0.126633207312847, -0.147452324368515, -0.116694958530379, 
   -0.0850414007120951, -0.141843551527024), FXI.Lag4 = c(-0.0644336250372613, 
   0.0619287501309259, -0.00181216391657935, 0.0975594992813728, 
   -0.119561028222494, -0.0346671754932032, -0.102042847003614, 
   -0.0732890353848151, -0.0874790314519183, -0.0772427814813398, 
   -0.0823272949467092, -0.0797431542266335, -0.0718297826940006
   ), FXI.Lag5 = c(-0.0622680074138888, 0.0358981393047052, 
   -0.010348556050472, -0.0220624757899933, -0.15775771623701, 
   -0.04434344401259, -0.127417284337483, -0.108205973707055, 
   -0.0854251578946245, -0.112119505385152, -0.0991033172551329, 
   -0.0855365894177914, -0.0741358857327016), FXI.P1 = c(0.0090818804807553, 
   -0.0269334335759555, -0.0195449730142365, -0.0685276542651128, 
   -0.0202036089057898, -0.00129946623658995, 0.166786049855342, 
   0.242115009565807, 0.147160799876093, 0.100922747462318, 
   0.163756345196888, 0.000808455394452649, 0.138616021443852
   ), FXI.P2 = c(0.0271459716522597, -0.0537509330277893, -0.0274165399923921, 
   -0.186174746520838, -0.0247400251061966, -0.0251797832481485, 
   0.240356336892926, 0.370756686091925, 0.223186626363536, 
   0.182108906559651, 0.242235717152391, 0.00884725192609069, 
   0.268868067541209), FXI.P3 = c(-0.173875860082961, 0.102706535338185, 
   0.00207673981546011, 0.618874902484494, -0.0951564545710784, 
   -0.0300668871102174, -0.285288888318339, -0.252753522901361, 
   -0.228980211728011, -0.240068181929505, -0.244104706729879, 
   -0.121671736596602, -0.309533088813122), FXI.P4 = c(0.187956804880432, 
   -0.108135519458274, 0.0178471643835552, -0.0412907430378404, 
   0.561513397654117, 0.287370742478992, 0.365998459449148, 
   0.396320093482696, 0.336150014981865, 0.278910292815561, 
   0.305125946857526, 0.327394904435326, 0.180169071191617), 
   FXI.P5 = c(0.154180329582612, -0.167189383429503, -0.0188482295802822, 
   -0.0431036604631461, 0.36117347370796, 0.387828646167237, 
   0.333005220408032, 0.374845425652877, 0.34931209227654, 0.2753158750552, 
   0.278370601589576, 0.226102734352802, 0.0740083949214576), 
   FXI.P6 = c(0.118597991167219, -0.112804776853844, -0.00513603689901898, 
   0.0110983010080525, 0.243869062855185, 0.267781240856836, 
   0.239120574861818, 0.29286360609434, 0.219084453277483, 0.216711328570966, 
   0.202621946492751, 0.214524354281793, 0.0763340986020788), 
   FXI.P7 = c(0.0871270874867273, -0.103673601364138, -0.0166056051257392, 
   -0.018204936475617, 0.204506735856291, 0.150564029406324, 
   0.193828729452497, 0.262374564678838, 0.200625009315957, 
   0.154975922357473, 0.211774704026319, 0.19131613483999, 0.120394002243309
   ), FXI.P8 = c(0.166597666971984, -0.160595790802746, -0.0183059515093151, 
   -0.129470227518549, 0.388181037592465, 0.346747943488679, 
   0.354730517356331, 0.39987035901151, 0.319105661746602, 0.313307434938073, 
   0.294846930242152, 0.347350374367261, 0.16942403445413), 
   FXI.P9 = c(0.127827694403684, -0.118529621668044, -0.00146628788229817, 
   -0.0542723060655054, 0.307529894194658, 0.217134313820781, 
   0.263309465355243, 0.34628937465282, 0.320171345860758, 0.257179898291487, 
   0.240347460951591, 0.224694002697233, 0.0687313877375792), 
   FXI.P10 = c(0.270915591372287, -0.232046645231559, -0.00816849692259382, 
   -0.328542935819366, 0.479809781761018, 0.348094770691552, 
   0.761746325373771, 1, 0.718262136349664, 0.673993572933146, 
   0.701156837833202, 0.404174762453734, 0.583673564477762), 
   EWU.Lag1 = c(-0.0795231653059828, 0.108744380536195, 0.0259255167636406, 
   0.557513017064625, -0.00851895176045262, -0.0109886675606758, 
   -0.188876673843354, -0.159866935184214, -0.171616225472188, 
   -0.131032045580866, -0.160357981626659, -0.0737646265670493, 
   -0.243658220235309), EWU.Lag2 = c(-0.110319374059469, 0.0651713091645725, 
   -0.0133304943872389, 0.436792474794412, -0.0568877097639174, 
   -0.0330087073538782, -0.192006074462363, -0.169230985167156, 
   -0.161230089990953, -0.16450409879482, -0.191376116819562, 
   -0.0922452694233632, -0.175310862090124), EWU.Lag3 = c(-0.0819412993510128, 
   0.0629508288802629, 0.00396432171021187, 0.283385997758425, 
   -0.122128925710327, -0.0376496207944432, -0.171550369158222, 
   -0.153796206353919, -0.156699937731305, -0.140529572222856, 
   -0.140583438603514, -0.101385493489756, -0.154037651719551
   ), EWU.Lag4 = c(-0.0925644479864173, 0.0660423957571441, 
   -0.0227075232553605, 0.144445801225261, -0.157898024643842, 
   -0.0405057422268177, -0.143495273786461, -0.103657810986652, 
   -0.156577032945983, -0.106498020084054, -0.122267076440174, 
   -0.0995450461816422, -0.108353790807439), EWU.Lag5 = c(-0.0797271164310339, 
   0.0104696700730029, -0.015362749987563, -0.0178233133707056, 
   -0.196656884038531, -0.0489895372463628, -0.156509079350662, 
   -0.115343465057333, -0.105947710587494, -0.135248969084368, 
   -0.110814791296953, -0.0976584866055188, -0.0962764215197402
   ), EWU.P1 = c(0.0149032950841066, -0.0226758426517318, -0.0348976063588681, 
   -0.0407201668820461, -0.0090530073248661, -0.0211246752059549, 
   0.0824293756565254, 0.0904049196106032, 0.101439501619524, 
   0.043749161016316, 0.0830769122904432, -0.0192089442825376, 
   0.0857686681770535), EWU.P2 = c(0.0200343254743678, -0.0410004291344403, 
   -0.0345172966006742, -0.131949748804342, -0.00524448016652099, 
   -0.0309415395254345, 0.144403844970348, 0.145761982793082, 
   0.172196070995576, 0.129923738730555, 0.171056889439732, 
   -0.000194945281298296, 0.147789099116271), EWU.P3 = c(-0.160368075266944, 
   0.146191196533574, 0.00888627130569298, 0.764798948368118, 
   -0.100044879508385, -0.0438582665990431, -0.31911664056832, 
   -0.273776091563777, -0.284345381579604, -0.247359152109571, 
   -0.283726762230195, -0.152287057833292, -0.339460292527805
   ), EWU.P4 = c(0.206182338289649, -0.123145858454248, 0.0428137423420082, 
   -0.0722310235669063, 0.647602768390241, 0.261143831119805, 
   0.434812069259549, 0.3541105545402, 0.498765105885862, 0.316758809247208, 
   0.375831950787619, 0.317980277831607, 0.245280222810527), 
   EWU.P5 = c(0.0686941979727608, -0.0742740840266079, 0.00426567197177591, 
   -0.0155035983664173, 0.189391644601218, 0.182554350488656, 
   0.160015224591799, 0.19098672774367, 0.31986037315408, 0.142477565553713, 
   0.193338070829032, 0.12554350865893, 0.0530519722706255), 
   EWU.P6 = c(0.156591881748298, -0.119367200852176, 0.0208217226902543, 
   0.036667210687425, 0.31071310357358, 0.500480252120222, 0.28718383838685, 
   0.248242982920007, 0.312105753993368, 0.266002554163154, 
   0.237321938413974, 0.377410807621257, 0.0642959104907023), 
   EWU.P7 = c(0.093283851723019, -0.0991539588272428, -0.000584391820393706, 
   0.0271866885514554, 0.207773009406125, 0.36541763028521, 
   0.197619463355462, 0.189682183857937, 0.246993496764527, 
   0.171739136217665, 0.164764217958918, 0.249206193222636, 
   0.103082809517883), EWU.P8 = c(0.22094776876955, -0.137293267740558, 
   0.0343243098907093, -0.0763856656929206, 0.431700186495602, 
   0.613444194499132, 0.393847688581539, 0.364630601530862, 
   0.438135315751089, 0.355737692026901, 0.347491432037734, 
   0.52329494438723, 0.160472738701643), EWU.P9 = c(0.13558698208684, 
   -0.113520501002071, 0.0260604541094746, -0.0164938355654745, 
   0.302236350144626, 0.389203423740901, 0.273796262834223, 
   0.210295759890275, 0.342793383917111, 0.233884493404256, 
   0.214402278372725, 0.186708294430902, 0.072316050893535), 
   EWU.P10 = c(0.304149438083611, -0.210387497808702, 0.028259127181329, 
   -0.339598372038162, 0.500349507703541, 0.319966750037057, 
   0.769011405166628, 0.718262136349664, 1, 0.669030839390801, 
   0.828995202349676, 0.365891471525231, 0.555601294077108), 
   EWJ.Lag1 = c(-0.116890743972305, 0.0765194130926833, -0.00708827178510057, 
   0.508698026492405, -0.00418487458855591, -0.0315408334288007, 
   -0.18363086862347, -0.173638762288708, -0.16139532969589, 
   -0.191521184680042, -0.158223039627632, -0.0638196614425742, 
   -0.221323589022923), EWJ.Lag2 = c(-0.132143184121054, 0.0522272398615334, 
   -0.026550971433514, 0.377439532119779, -0.0451650022255943, 
   -0.049659777660805, -0.190329051490367, -0.168533384332445, 
   -0.159861128235065, -0.181504899747166, -0.183315150350997, 
   -0.0825137248808647, -0.191734618992461), EWJ.Lag3 = c(-0.0570980394481774, 
   0.0684836995485838, 0.0235175427564965, 0.236345347928015, 
   -0.0925067632595704, -0.0576115146042475, -0.176984224520279, 
   -0.138495964423575, -0.131053303463752, -0.147476727989109, 
   -0.126872947243225, -0.091413337614465, -0.130405272519946
   ), EWJ.Lag4 = c(-0.0803901091053835, 0.0707680484054203, 
   -0.00269532369444101, 0.130238308298671, -0.1262397057696, 
   -0.0691829118691654, -0.127027942204026, -0.0876893889004994, 
   -0.125302373991434, -0.0886380565198913, -0.103926883764225, 
   -0.0935647337240292, -0.0768382206169777), EWJ.Lag5 = c(-0.103924623148355, 
   0.00792077205072304, -0.0273702281361436, -0.00276466409590761, 
   -0.185404677714953, -0.0808203622351356, -0.150669445006382, 
   -0.119597893068918, -0.0948787342130669, -0.125832600967221, 
   -0.0931364842321882, -0.0997222327355824, -0.0913734151484278
   ), EWJ.P1 = c(0.0124638974619449, -0.0420122405586138, -0.0127232803429817, 
   -0.10123938231803, -0.00564955953779341, 0.00168435857230873, 
   0.1394497779528, 0.127482067405044, 0.133964591175914, 0.139936832737881, 
   0.147650493711596, -0.00848194150746332, 0.109121476551159
   ), EWJ.P2 = c(0.0454992371455172, -0.0568055838579423, -0.0124953448347487, 
   -0.206306696843678, -0.0251670242348876, -0.0223318478347899, 
   0.214789634292326, 0.207603802728917, 0.202969464871072, 
   0.235202512887577, 0.227809981202658, 0.0055314106777738, 
   0.224416865764573), EWJ.P3 = c(-0.197845381762546, 0.125026421899885, 
   -0.0119815768127731, 0.71369602548092, -0.080874361448684, 
   -0.0821401107245415, -0.331260565035353, -0.289296095935403, 
   -0.279704807783019, -0.312034149863545, -0.285856313913704, 
   -0.142195127458079, -0.336482612149306), EWJ.P4 = c(0.224295902285116, 
   -0.153711808155587, 0.0320466365966449, -0.0661697060629079, 
   0.63965584563097, 0.390516948348952, 0.461033382993538, 0.424525955352048, 
   0.437346792530305, 0.486939228522318, 0.365915132068192, 
   0.366956149713899, 0.193311650445247), EWJ.P5 = c(0.105548302298, 
   -0.10272965700888, 0.00861728770242395, -0.00114944992518017, 
   0.247228736128038, 0.326557263916526, 0.222754230356379, 
   0.281751285220782, 0.2996397813992, 0.298639625728844, 0.202367622861038, 
   0.0806220455944047, -0.0591601832419126), EWJ.P6 = c(0.105119514782628, 
   -0.0849583949714317, 0.0185329144883777, 0.050498752276506, 
   0.210552783281014, 0.387307086175443, 0.206286126002358, 
   0.238170638428562, 0.199137013732189, 0.3203177203706, 0.175115238942863, 
   0.16380014076889, -0.0277719264915061), EWJ.P7 = c(0.0453073021388557, 
   -0.0355800852620458, 0.010230193051766, 0.0369119121884645, 
   0.0825785014599144, 0.0855510760656762, 0.0835637051823488, 
   0.105972461551025, 0.102788647807727, 0.187132012243823, 
   0.115171933783893, -0.0271079173415519, -0.0274746877939691
   ), EWJ.P8 = c(0.225263201288374, -0.14221656074666, 0.0457655015740676, 
   -0.0258322290986102, 0.427029945579948, 0.600412572018819, 
   0.397446731264196, 0.398473136630882, 0.349583750203456, 
   0.463007492884456, 0.299497550268872, 0.368401169699482, 
   0.0914151490088255), EWJ.P9 = c(0.136842531040462, -0.0993673715085427, 
   0.0345020944982582, 0.0276340491274045, 0.273527463843426, 
   0.361635478829549, 0.251459559128175, 0.293421625977407, 
   0.324042839691366, 0.337024015510759, 0.276618260491423, 
   0.132888297979385, -0.012046438290244), EWJ.P10 = c(0.250990741135676, 
   -0.242548589975152, -0.0215494421400653, -0.345925583724904, 
   0.463204298201201, 0.379805515433103, 0.743850374453961, 
   0.673993572933146, 0.669030839390801, 1, 0.662071616455537, 
   0.328857228678916, 0.466635692990533), EWG.Lag1 = c(-0.112973457367362, 
   0.0822942585346857, -0.0083515631764973, 0.526900952051509, 
   -0.0188518184513795, 0.00482063564020835, -0.185597289080095, 
   -0.16117959924531, -0.186091949616437, -0.128574924926931, 
   -0.174649026221404, -0.0519621791531982, -0.241942301450646
   ), EWG.Lag2 = c(-0.0880326485796015, 0.0732893919943342, 
   0.00950060979525044, 0.387897485529562, -0.0666680287011539, 
   -0.014822251864415, -0.192508374399598, -0.15416648269785, 
   -0.167220660262513, -0.158914965306165, -0.202170772118921, 
   -0.0720966084118441, -0.176329373848071), EWG.Lag3 = c(-0.0803719074375688, 
   0.0646518700680633, 0.0114821208201095, 0.269648326483838, 
   -0.121794959283517, -0.0213469524504794, -0.16136109364544, 
   -0.133657366790745, -0.118098469078234, -0.131047902419088, 
   -0.12002880012833, -0.087876458288623, -0.142676726035335
   ), EWG.Lag4 = c(-0.0912583206338523, 0.0794548540262304, 
   -0.00588284133205231, 0.149301877141332, -0.147451372113228, 
   -0.0297435528341411, -0.143544029492261, -0.0968244855770464, 
   -0.134380791016073, -0.0963442992757264, -0.13708322256626, 
   -0.0851965253916176, -0.0998725075984432), EWG.Lag5 = c(-0.0878413931559876, 
   0.00888998282882959, -0.0193903887556184, 0.00118599787153956, 
   -0.194630877112547, -0.0433535108967198, -0.1705437375134, 
   -0.133898106557242, -0.115909265186332, -0.146268708367119, 
   -0.149497044680193, -0.0893522148755781, -0.118023023171738
   ), EWG.P1 = c(-0.0371841520713633, -0.07795726542374, -0.062539260804832, 
   -0.0333111444866914, 0.0129981351685979, 0.015885433037365, 
   0.104212638782493, 0.111477912418432, 0.103508890424311, 
   0.0727949386794126, 0.130980191862521, -0.00582836871572684, 
   0.0957074487634646), EWG.P2 = c(-0.0297244941179566, -0.098093570158792, 
   -0.0649813954673873, -0.152429842704776, -0.0131513175726123, 
   -0.0362617202177008, 0.17307658223011, 0.16500815605539, 
   0.153910012254519, 0.136551951255596, 0.21767194297438, -0.00912591432381617, 
   0.194333128409117), EWG.P3 = c(-0.177306497485761, 0.138374958317804, 
   0.00221332984293543, 0.73175896754357, -0.116002744752713, 
   -0.0155542967680099, -0.323831983462172, -0.26719453400888, 
   -0.291914157366025, -0.245214920122467, -0.304407703174808, 
   -0.122557983360424, -0.343768872749024), EWG.P4 = c(0.190427230032568, 
   -0.125252097699692, 0.0396991137345505, -0.0673114944381348, 
   0.650768985610459, 0.247965035608752, 0.424002913925095, 
   0.325556216654839, 0.415096774514741, 0.297836076331447, 
   0.408892881958065, 0.302015830732654, 0.229280815586321), 
   EWG.P5 = c(0.0981745122893209, -0.103833923833139, 0.00166095431411162, 
   -0.021075638416033, 0.257929483072538, 0.282052705719759, 
   0.221669415043949, 0.23046083839253, 0.316200907329648, 0.206136140149477, 
   0.238003995150114, 0.158821418831286, 0.0391849940815457), 
   EWG.P6 = c(0.119348631138642, -0.0925772974567481, 0.0219119439494785, 
   0.0544022438178669, 0.238251396431227, 0.52108338703829, 
   0.228748382856292, 0.206032940676773, 0.238434315530609, 
   0.219296463166383, 0.185010821459836, 0.360093224277514, 
   0.0499409854411404), EWG.P7 = c(0.0360284230628375, -0.050439161279797, 
   0.00830488535889297, 0.0438947357652542, 0.0995947085234199, 
   0.334664675401958, 0.0933825664325496, 0.0862655325890056, 
   0.10145146160819, 0.0849640896429375, 0.0284894072887891, 
   0.128847227964833, 0.0214440371162281), EWG.P8 = c(0.210227955095491, 
   -0.167936241836018, 0.0212105188961211, -0.113679508365062, 
   0.423245302166215, 0.598709455593141, 0.415373033959048, 
   0.371714494121741, 0.392437296644423, 0.389801350976424, 
   0.356804954132897, 0.457545853416409, 0.163321975015479), 
   EWG.P9 = c(0.124439589722836, -0.0969056962956594, 0.0241329756823627, 
   -0.0148387509091088, 0.266097923694627, 0.342950079819879, 
   0.240572021184161, 0.184844156840365, 0.259837042949623, 
   0.206789341171586, 0.183466037210653, 0.133975978014064, 
   0.0227157713193279), EWG.P10 = c(0.270511342803479, -0.216064781374194, 
   0.0109568583727757, -0.340849994214403, 0.450284175224949, 
   0.260961836399518, 0.738218044554685, 0.701156837833202, 
   0.828995202349676, 0.662071616455537, 1, 0.328974230311909, 
   0.565691929489326), VIX.Lag1 = c(0.0945046885236358, -0.0622690509559807, 
   -0.00265652025802362, -0.532784159822371, -0.116769405217141, 
   -0.0342474703670779, 0.14506280857851, 0.105504353790513, 
   0.112086363787519, 0.0965507854884891, 0.125214785993872, 
   -0.012573579884546, 0.2277327159061), VIX.Lag2 = c(0.0891748320771217, 
   -0.0689278312955687, -0.00396996688235431, -0.416586075307238, 
   -0.0463033340686871, -0.0228870905284603, 0.154392317007114, 
   0.135700413087404, 0.129873873551607, 0.140168482912925, 
   0.157145943753422, 0.0121650126667914, 0.189240357239906), 
   VIX.Lag3 = c(0.0383994702630361, -0.0790548007910768, -0.0390211770630212, 
   -0.295650689953981, 0.0248299904704649, -0.0180738779647492, 
   0.157439712167223, 0.145760815763991, 0.127539285031424, 
   0.123603159454959, 0.116956002007018, 0.0297274706668955, 
   0.160214867807208), VIX.Lag4 = c(0.0663456361358825, -0.0603445474172778, 
   0.0147540555647369, -0.18481450774408, 0.0832646601720761, 
   -0.000592480838502963, 0.121868416862094, 0.0850262259908515, 
   0.111689512493149, 0.102398954072543, 0.0987409139677846, 
   0.0328247650149329, 0.0983810982272812), VIX.Lag5 = c(0.087935145272097, 
   0.012883857307251, 0.0460053507141171, -0.0104877527753627, 
   0.165666056819521, 0.00985542074620365, 0.127472943854081, 
   0.0897480899993805, 0.0899636249940871, 0.119927893745412, 
   0.0935796680224021, 0.0436916306472499, 0.0901745048846342
   ), VIX.P1 = c(-0.0224248829500832, -0.0376354778566962, -0.0393890280720117, 
   -0.0570166343796589, -0.0167202556677217, -0.0324622652108995, 
   0.17121234852312, 0.153899285524911, 0.143860297022976, 0.1150096355225, 
   0.155134429816379, -0.0191857198165952, 0.114299580141159
   ), VIX.P2 = c(0.0315177341387727, -0.0768363282796056, -0.0458684987075317, 
   -0.215367718512824, -0.00578761168555371, -0.034824982517886, 
   0.264334253125177, 0.25153892750009, 0.248836609220222, 0.21267049841631, 
   0.243623924829571, -0.000369403954100229, 0.226690335667394
   ), VIX.P3 = c(0.178438552503406, -0.15179818597003, -0.0156315372304355, 
   -0.817336324192968, -0.0158791512359489, -0.023166693662438, 
   0.357237850458839, 0.293694176340498, 0.294031117292004, 
   0.272421282544014, 0.31328486212125, 0.0936811429202996, 
   0.47271892887804), VIX.P4 = c(0.22139737453634, -0.142728565713746, 
   0.026282906344604, -0.154857677944532, 0.73326115808032, 
   0.174017732152649, 0.476596164604283, 0.348384683190196, 
   0.364414756758686, 0.289009409413546, 0.356968690840127, 
   0.447766131877873, 0.48951924833156), VIX.P5 = c(0.156196063385693, 
   -0.101068971019945, 0.0169263629147121, -0.0116021050662099, 
   0.3495228597214, 0.385916489239089, 0.284370511565794, 0.2161694989732, 
   0.210765232798837, 0.14307985623001, 0.170294463277075, 0.397024316592066, 
   0.210876185436321), VIX.P6 = c(0.235546136830897, -0.145005330176965, 
   0.0239417271422657, -0.13067540904444, 0.472749553258322, 
   0.647808076662489, 0.428700920236045, 0.404174762453734, 
   0.365891471525231, 0.328857228678916, 0.328974230311909, 
   1, 0.46049971479368), VIX.P7 = c(0.18379775529555, -0.14282146612437, 
   0.00302637651308501, -0.0831223240700085, 0.384239001164054, 
   0.454515554898604, 0.357112315166059, 0.312947255066963, 
   0.262504695485812, 0.204464091590193, 0.21998943821733, 0.613494296139021, 
   0.369193945587804), VIX.P8 = c(0.230228960274104, -0.112237216132465, 
   0.0529508386597007, 0.0442785558635877, 0.448957787047278, 
   0.67070451238034, 0.369800112589417, 0.303285717771382, 0.278964863955144, 
   0.262046169174222, 0.246997103325596, 0.672946134852533, 
   0.19805406468399), VIX.P9 = c(0.117098339528548, -0.0921548524722941, 
   0.0228649605617307, 0.0297402561970922, 0.256642106468211, 
   0.396122432390819, 0.224258118755018, 0.139105862076813, 
   0.149142473030217, 0.104704346663312, 0.101901251024383, 
   0.12099903556422, 0.0811070797407164), VIX.P10 = c(0.234221042704035, 
   -0.171130416710285, -0.00059954020618912, -0.428609440054892, 
   0.383491037280305, 0.157500505133063, 0.661110022922387, 
   0.583673564477762, 0.555601294077108, 0.466635692990533, 
   0.565691929489326, 0.46049971479368, 1)), row.names = c("y_High", 
"y_Low", "y_Today", "P3", "P4", "P8", "P10", "FXI.P10", "EWU.P10", 
"EWJ.P10", "EWG.P10", "VIX.P6", "VIX.P10"), class = "data.frame")

Okay, thanks for sharing the data. You can decide which pair-wise comparisons you want to show. You said you have selected the top 10 variables, but I wasn't sure which ones those are.

# This code re-structures your data to long format
data_long <- 
  data %>% 
  as_tibble(rownames = 'x') %>% 
  pivot_longer(-1, names_to = 'y', values_to = 'cor') %>% 
  mutate(pair = glue::glue('{x} vs. {y}'),
         pair = fct_reorder(pair, cor))

# bar plot
data_long %>% 
  slice(1:10) %>% # only selecting first 10 rows
  ggplot(aes(pair, cor)) +
  geom_col() +
  coord_flip()

Is this what you are trying to accomplish?

library(ggplot2)
library(dplyr)

# Sample data
df <- data.frame(stringsAsFactors=FALSE,
   correlations = c(0.27345653762845, 0.310765738936609, 0.242204371067535,
                    0.356047857951224, 0.270915591372287, 0.304149438083611,
                    0.250990741135676, 0.270511342803479, 0.235546136830897,
                    0.234221042704035),
             nm = c("P3", "P4", "P8", "P10", "FXI.P10", "EWU.P10", "EWJ.P10",
                    "EWG.P10", "VIX.P6", "VIX.P10")
)

ggplot(df, aes(x = reorder(nm, desc(correlations)), y = correlations, fill = nm)) +
    geom_col(show.legend = FALSE) +
    labs(title = "Top 10 Correlations",
         x = "Variable Name",
         y = "Correlation") +
    theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))

Yes! Thats exactly what I was looking for. Do you mind explaining exactly how you did it?

This are my assumptions. Correct me if I am wrong:
1.stringsAsFactors=FALSE means that you made everything numeric
2. Using the nm and correlation functions, you are creating your own x and y variables based on the results of the corr function in order to plot
3. desc means descending?
4. what does the labs function does?
5. what function made every column had each color?

Sorry for asking to many questions. I just want to make sure I understand the process in case I have to do it again in the future

Thank you so much for your help. I really appreciate it!

This is telling data.frame() function, not to consider text variables as factors, this is usually a good practice but in this case, it is actually not relevant.

Those are not functions, in the context fo my example, those are column names representing the vectors that you are creating with your code, in your case you could create a data frame from those vectors with this command

df <- data.frame(correlations, nm)

Yes

It defines the labels for the plot, like the title, x-axis label, and y-axis label.

I have mapped the nm variable to the fill aesthetic, this is done inside the aes() function, if you want to learn how this works I recommend you to read this chapter of R4DS

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.