有没有办法将相关矩阵拆分为只显示它的某个部分 (R)?
Is there a way to split a correlation matrix to only display a certain section of it (R)?
我有一个正在使用的数据集(在本例中为随机数据),我只需要显示矩阵的特定部分。有没有办法保持代码中指定的相同参数,但只显示我需要的矩阵部分?
我将在下面显示的代码生成此处显示的矩阵:
我喜欢所有东西的大小,尤其是右侧的 Pearson 相关值图例。但是,我只需要显示下图中红色框内的所有内容:
这是我正在使用的代码:
数据:
sen <- structure(list(STAGE3 = c(0.876917687075825, 0.412349699244156,
0.0276263864194565, 0.715878164202211, 0.113324379019157, 0.697977125373629,
0.495239700082692, 0.433931430983351, 0.969730865766935, 0.325140127448327,
0.571408271249494, 0.795998354603895, 0.677190885671071, 0.725950370073618,
0.773033245228681, 0.124492827494149, 0.392125485039593, 0.293970448598711,
0.574176683397314, 0.439175490268417, 0.187274945963726, 0.114361533064052,
0.984810340190489, 0.094824786617737, 0.365076362766766, 0.484529293489265,
0.0248352738577235, 0.370080530701056, 0.916058724203794, 0.698768788065752,
0.0486805863644058, 0.417384720565167, 0.575510045429799, 0.306442633779882,
0.352619466947156, 0.0468312121293604, 0.719674300918072, 0.245979619378491,
0.497903453826379, 0.815929672015352, 0.579266045428163, 0.493374660814721,
0.981395698013249, 0.501428573306618, 0.232710091203988, 0.295487027432436,
0.0358089940602796, 0.122221799715662, 0.475075779687791, 0.64716894430911,
0.411728227914661, 0.189801935164384, 0.995784139092795, 0.338669053292779,
0.888464605534105, 0.727167494736264, 0.359872858562472, 0.501785825883555,
0.131682859300131, 0.316615799479412), STAGE2 = c(0.731671739637962,
0.0349659624268288, 0.250468754401736, 0.692725665227725, 0.63127309683438,
0.267791520174364, 0.683510378409481, 0.0930250575768417, 0.0469303813447592,
0.664296422188774, 0.104791391299402, 0.769846767284999, 0.6757286415883,
0.752123872001356, 0.90542496106528, 0.495894318363979, 0.693023609703169,
0.268140720274296, 0.715499146536921, 0.28736047756967, 0.0734999857226017,
0.220321023927838, 0.168970847093894, 0.329637479054441, 0.932333806354766,
0.920347276344009, 0.0188743828318305, 0.28700830085674, 0.122086809397366,
0.721930469997516, 0.269049759688981, 0.942247445889545, 0.121226461766639,
0.269410103956184, 0.543882827865607, 0.355909915494972, 0.805553875119072,
0.562638932909877, 0.545114635056338, 0.799859513505137, 0.028300052307727,
0.583254546237197, 0.256765686729071, 0.611805711034515, 0.12397867417326,
0.904971687781288, 0.383547195151525, 0.798974934455656, 0.808113210022865,
0.180237564596422, 0.523175753649262, 0.429905001793785, 0.304352173583082,
0.40672229380892, 0.216918826340633, 0.995255476036526, 0.769882865282478,
0.315877004728925, 0.473236774091463, 0.977350069130875), STAGE1 = c(0.368965432370926,
0.700308646686629, 0.37243236843735, 0.118248974397151, 0.924365141290828,
0.762350776297416, 0.697822321048335, 0.319088598768702, 0.877683213554975,
0.750204754502701, 0.504154725402079, 0.142208638643596, 0.789965310283345,
0.23064744899036, 0.356016354550943, 0.62336479144862, 0.213046955322873,
0.0244722022581561, 0.988358158842575, 0.823084572956891, 0.303622687500738,
0.96764100797214, 0.230471412999795, 0.817462150200591, 0.65596635231198,
0.91076738319561, 0.819508996182778, 0.213131722185214, 0.588018916178037,
0.123125204015868, 0.562792521212946, 0.660044396503998, 0.978415415705398,
0.0434221393194675, 0.376954390238487, 0.0621651663358314, 0.465209931429691,
0.384575866404939, 0.242944268682905, 0.0795230807069031, 0.0199700669678636,
0.32561072886232, 0.93772541505141, 0.585428916722007, 0.318073982629541,
0.32173669386666, 0.999051218264009, 0.317443552571237, 0.660217053596061,
0.823899098997627, 0.514457733357758, 0.541111096768051, 0.55508809891723,
0.984535365206941, 0.471258908268993, 0.993506687275954, 0.437682961713191,
0.131816605892007, 0.624970126586813, 0.464518729700115), VEG1 = c(0.210969928562774,
0.898985728644631, 0.22294741757493, 0.648093601120814, 0.575025933877221,
0.836263821166674, 0.365992846024045, 0.975590314436119, 0.7669346163546,
0.606281773283188, 0.727857331528672, 0.063336204942156, 0.56024387105928,
0.572624926379006, 0.928959824695475, 0.0649894364822472, 0.484687521083679,
0.0408377810735494, 0.0863494174729038, 0.38604744652937, 0.408453836726946,
0.886479379706545, 0.484111782315899, 0.931864491830317, 0.822804779811041,
0.42950876407607, 0.870394703931469, 0.643439731712405, 0.9667566201692,
0.304607549846345, 0.364859919775087, 0.103200556844174, 0.548045455964784,
0.198120269406476, 0.0343580658743184, 0.802310877023954, 0.993783336617898,
0.0816308254407008, 0.75765020441394, 0.864780385461517, 0.312678655208107,
0.74836775788609, 0.534055051243098, 0.288890283554979, 0.953079521748438,
0.731857197421753, 0.142177987385593, 0.996762443657084, 0.243292464894029,
0.491579671466928, 0.966627062162325, 0.339857447154563, 0.461728147289064,
0.634729865229662, 0.3609970911949, 0.258390752729148, 0.506726536795493,
0.672180274413292, 0.747445719779432, 0.29334486037021), VEG2 = c(0.380862280240474,
0.627729953103904, 0.878278401019092, 0.5613186824923, 0.636249763005486,
0.62292681302113, 0.720444249110863, 0.312557900422543, 0.234122422714287,
0.499639072408177, 0.855974632998296, 0.952555098662679, 0.309983642251446,
0.882338335501354, 0.361924385825836, 0.0113958292196743, 0.302133129735417,
0.0208791658903581, 0.117232590999101, 0.925267325435408, 0.0711269914368884,
0.552243014270943, 0.125495115593106, 0.661915190374669, 0.0293896936238214,
0.294418615555011, 0.308905573205888, 0.26936911134833, 0.931505940770319,
0.021435120451226, 0.227312617286182, 0.0195785283931383, 0.099692235112247,
0.0692925181417872, 0.832555037914636, 0.380793087982671, 0.68110703188308,
0.0936612801494288, 0.051047767208204, 0.273844437996178, 0.800343046949624,
0.911380903925364, 0.582479041522435, 0.748111458524328, 0.303383302981895,
0.906904782788438, 0.328934737098847, 0.69720659917789, 0.166551542287389,
0.875547286349842, 0.265071728355448, 0.698643970290581, 0.271578241223373,
0.956035715886986, 0.583251175640849, 0.268613096173177, 0.738778731483354,
0.22125582045959, 0.289055369557255, 0.697577213911879), VEG3 = c(0.413110318831049,
0.193370500853792, 0.0443323484313173, 0.312394866761789, 0.381013563679286,
0.774340211752776, 0.315822280223384, 0.499646073445189, 0.0433440470476795,
0.63592018453082, 0.375881818437784, 0.758815519342307, 0.517769074796318,
0.0878879638478763, 0.368268890348083, 0.86661723542387, 0.159257788537873,
0.302684958923527, 0.947570919817041, 0.157392788910852, 0.125769229458094,
0.348650174982873, 0.526963940972057, 0.632646354644645, 0.351109914949622,
0.836552506892888, 0.541982479414601, 0.427697355405521, 0.799356060602721,
0.205934189654569, 0.511986141383475, 0.807482528548695, 0.232665456121877,
0.933325748417277, 0.00683041685826713, 0.21579707708936, 0.92805622685536,
0.754838770281907, 0.39901873988239, 0.991423691630203, 0.73157540991456,
0.572610353705451, 0.806848128394244, 0.445431445389894, 0.26939539811961,
0.584527298222492, 0.386004220382543, 0.242375752770125, 0.558781312238412,
0.026491283834456, 0.343044539065029, 0.802187562912211, 0.885495299962394,
0.853256462542473, 0.126077883381321, 0.780548336023421, 0.840250789403559,
0.724812876802032, 0.704098036072535, 0.201989727535322), VEG4 = c(0.640799340986514,
0.987906611244886, 0.619024206202544, 0.209427026032481, 0.489727139189696,
0.687333141302772, 0.705507906383631, 0.208889941178721, 0.396874200897516,
0.712945929935184, 0.973888093276408, 0.763823354263263, 0.377489995555215,
0.0106930906722855, 0.803664402691951, 0.278756822036515, 0.0550468580770419,
0.265168521344556, 0.987573377094547, 0.734619443919844, 0.732929467499789,
0.241731972053084, 0.559823481667716, 0.155701349465554, 0.478406027901528,
0.961160568832851, 0.0998154836815058, 0.412094044212779, 0.243883425544374,
0.942308621371675, 0.939264039325847, 0.255191997669476, 0.25098970135836,
0.882652992136823, 0.287854570522429, 0.332318399664456, 0.412416503681846,
0.321868386633491, 0.415802982737621, 0.518373582745234, 0.160366074863356,
0.328725259142546, 0.740582698190321, 0.630219424448588, 0.0963326376312392,
0.841597785551852, 0.157610936357946, 0.37344035882236, 0.665277345336568,
0.472350881237637, 0.905421464878138, 0.67001107287601, 0.931361601273204,
0.478463787477743, 0.457093329803116, 0.901963213218536, 0.0989040910780277,
0.648496145325191, 0.149756719258531, 0.766506521363941), VEG5 = c(0.180283594647103,
0.787728519142257, 0.577806490890149, 0.909505719647681, 0.843259853705306,
0.19170485676349, 0.88285289130116, 0.390956223533144, 0.400453739302465,
0.138993973168619, 0.701071911445177, 0.0275528476660395, 0.196129789901458,
0.243754073834767, 0.586994020311301, 0.309287604588564, 0.54116171026274,
0.804029207727762, 0.633322226579327, 0.576488317293166, 0.657529806993353,
0.896942372401784, 0.69552906585738, 0.00605796084044197, 0.0333828607443704,
0.421697067898017, 0.344727073066524, 0.261778629704536, 0.662947164033825,
0.264729400387399, 0.361422182455181, 0.867093056031147, 0.994741928030988,
0.1442062746899, 0.757388857488693, 0.0417225005179914, 0.448764060787373,
0.844033548534798, 0.615968575276885, 0.424045632312236, 0.419379052486891,
0.47584349576189, 0.209702827083009, 0.765825214487151, 0.834675742228314,
0.1887294652982, 0.960231969290817, 0.935011727569393, 0.976674243295263,
0.820848463468705, 0.341388666484707, 0.552464582497579, 0.573838987965132,
0.698330744449092, 0.383783839725491, 0.713851582153272, 0.964603044208545,
0.70783623057194, 0.608167299267236, 0.461582775185318)), class = "data.frame", row.names = c(NA,
-60L))
代码:
library(ggplot2)
library(ggcorrplot)
library(corrplot)
#Generate correlation matrix
res <- cor(sen)
#Generate matrix graphic
ggcorrplot(res, hc.order = FALSE, type = "lower",
outline.col = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", "white", "#E46726"),
lab = TRUE, lab_size = 7, show.diag=FALSE,
legend.title = "r-value", tl.cex=17)+
theme(
legend.key.width=unit(1, "cm"),
legend.key.height=unit(2.5, "cm"),
legend.title=element_text(size=20),
legend.text=element_text(size=15)
)
library(reshape2)
library(ggplot2)
res <- cor(sen)
res[upper.tri(res)] = NA
diag(res) = NA
d = melt(res)
d = d[d$Var2 %in% paste0("STAGE", 1:3),]
graphics.off()
ggplot(d, aes(x = Var1, y = Var2, fill = value, label = round(value, 2))) +
geom_tile(color = "black") +
geom_text() +
labs(fill = "r-value") +
coord_equal() #OPTIONAL
我发现另一种解决方案是将比例调整到可以裁剪掉不需要的变量的位置。这不是最技术性的答案,但它适用于我需要的参数:
png("wholeplant.png", width=900, height=600)
ggcorrplot(res, hc.order = FALSE, type = "lower",
outline.col = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", "white", "#E46726"),
lab = TRUE, lab_size = 7, show.diag=FALSE,
legend.title = "r-value", tl.cex=17)+
theme(
#resize the legend width and height
legend.key.width=unit(1, "cm"),
legend.key.height=unit(1, "cm"),
legend.title=element_text(size=20),
legend.text=element_text(size=15),
#Put it where it won't be out of the way
legend.position = c(1.1, 0.2)
)
dev.off()
我有一个正在使用的数据集(在本例中为随机数据),我只需要显示矩阵的特定部分。有没有办法保持代码中指定的相同参数,但只显示我需要的矩阵部分?
我将在下面显示的代码生成此处显示的矩阵:
我喜欢所有东西的大小,尤其是右侧的 Pearson 相关值图例。但是,我只需要显示下图中红色框内的所有内容:
这是我正在使用的代码:
数据:
sen <- structure(list(STAGE3 = c(0.876917687075825, 0.412349699244156,
0.0276263864194565, 0.715878164202211, 0.113324379019157, 0.697977125373629,
0.495239700082692, 0.433931430983351, 0.969730865766935, 0.325140127448327,
0.571408271249494, 0.795998354603895, 0.677190885671071, 0.725950370073618,
0.773033245228681, 0.124492827494149, 0.392125485039593, 0.293970448598711,
0.574176683397314, 0.439175490268417, 0.187274945963726, 0.114361533064052,
0.984810340190489, 0.094824786617737, 0.365076362766766, 0.484529293489265,
0.0248352738577235, 0.370080530701056, 0.916058724203794, 0.698768788065752,
0.0486805863644058, 0.417384720565167, 0.575510045429799, 0.306442633779882,
0.352619466947156, 0.0468312121293604, 0.719674300918072, 0.245979619378491,
0.497903453826379, 0.815929672015352, 0.579266045428163, 0.493374660814721,
0.981395698013249, 0.501428573306618, 0.232710091203988, 0.295487027432436,
0.0358089940602796, 0.122221799715662, 0.475075779687791, 0.64716894430911,
0.411728227914661, 0.189801935164384, 0.995784139092795, 0.338669053292779,
0.888464605534105, 0.727167494736264, 0.359872858562472, 0.501785825883555,
0.131682859300131, 0.316615799479412), STAGE2 = c(0.731671739637962,
0.0349659624268288, 0.250468754401736, 0.692725665227725, 0.63127309683438,
0.267791520174364, 0.683510378409481, 0.0930250575768417, 0.0469303813447592,
0.664296422188774, 0.104791391299402, 0.769846767284999, 0.6757286415883,
0.752123872001356, 0.90542496106528, 0.495894318363979, 0.693023609703169,
0.268140720274296, 0.715499146536921, 0.28736047756967, 0.0734999857226017,
0.220321023927838, 0.168970847093894, 0.329637479054441, 0.932333806354766,
0.920347276344009, 0.0188743828318305, 0.28700830085674, 0.122086809397366,
0.721930469997516, 0.269049759688981, 0.942247445889545, 0.121226461766639,
0.269410103956184, 0.543882827865607, 0.355909915494972, 0.805553875119072,
0.562638932909877, 0.545114635056338, 0.799859513505137, 0.028300052307727,
0.583254546237197, 0.256765686729071, 0.611805711034515, 0.12397867417326,
0.904971687781288, 0.383547195151525, 0.798974934455656, 0.808113210022865,
0.180237564596422, 0.523175753649262, 0.429905001793785, 0.304352173583082,
0.40672229380892, 0.216918826340633, 0.995255476036526, 0.769882865282478,
0.315877004728925, 0.473236774091463, 0.977350069130875), STAGE1 = c(0.368965432370926,
0.700308646686629, 0.37243236843735, 0.118248974397151, 0.924365141290828,
0.762350776297416, 0.697822321048335, 0.319088598768702, 0.877683213554975,
0.750204754502701, 0.504154725402079, 0.142208638643596, 0.789965310283345,
0.23064744899036, 0.356016354550943, 0.62336479144862, 0.213046955322873,
0.0244722022581561, 0.988358158842575, 0.823084572956891, 0.303622687500738,
0.96764100797214, 0.230471412999795, 0.817462150200591, 0.65596635231198,
0.91076738319561, 0.819508996182778, 0.213131722185214, 0.588018916178037,
0.123125204015868, 0.562792521212946, 0.660044396503998, 0.978415415705398,
0.0434221393194675, 0.376954390238487, 0.0621651663358314, 0.465209931429691,
0.384575866404939, 0.242944268682905, 0.0795230807069031, 0.0199700669678636,
0.32561072886232, 0.93772541505141, 0.585428916722007, 0.318073982629541,
0.32173669386666, 0.999051218264009, 0.317443552571237, 0.660217053596061,
0.823899098997627, 0.514457733357758, 0.541111096768051, 0.55508809891723,
0.984535365206941, 0.471258908268993, 0.993506687275954, 0.437682961713191,
0.131816605892007, 0.624970126586813, 0.464518729700115), VEG1 = c(0.210969928562774,
0.898985728644631, 0.22294741757493, 0.648093601120814, 0.575025933877221,
0.836263821166674, 0.365992846024045, 0.975590314436119, 0.7669346163546,
0.606281773283188, 0.727857331528672, 0.063336204942156, 0.56024387105928,
0.572624926379006, 0.928959824695475, 0.0649894364822472, 0.484687521083679,
0.0408377810735494, 0.0863494174729038, 0.38604744652937, 0.408453836726946,
0.886479379706545, 0.484111782315899, 0.931864491830317, 0.822804779811041,
0.42950876407607, 0.870394703931469, 0.643439731712405, 0.9667566201692,
0.304607549846345, 0.364859919775087, 0.103200556844174, 0.548045455964784,
0.198120269406476, 0.0343580658743184, 0.802310877023954, 0.993783336617898,
0.0816308254407008, 0.75765020441394, 0.864780385461517, 0.312678655208107,
0.74836775788609, 0.534055051243098, 0.288890283554979, 0.953079521748438,
0.731857197421753, 0.142177987385593, 0.996762443657084, 0.243292464894029,
0.491579671466928, 0.966627062162325, 0.339857447154563, 0.461728147289064,
0.634729865229662, 0.3609970911949, 0.258390752729148, 0.506726536795493,
0.672180274413292, 0.747445719779432, 0.29334486037021), VEG2 = c(0.380862280240474,
0.627729953103904, 0.878278401019092, 0.5613186824923, 0.636249763005486,
0.62292681302113, 0.720444249110863, 0.312557900422543, 0.234122422714287,
0.499639072408177, 0.855974632998296, 0.952555098662679, 0.309983642251446,
0.882338335501354, 0.361924385825836, 0.0113958292196743, 0.302133129735417,
0.0208791658903581, 0.117232590999101, 0.925267325435408, 0.0711269914368884,
0.552243014270943, 0.125495115593106, 0.661915190374669, 0.0293896936238214,
0.294418615555011, 0.308905573205888, 0.26936911134833, 0.931505940770319,
0.021435120451226, 0.227312617286182, 0.0195785283931383, 0.099692235112247,
0.0692925181417872, 0.832555037914636, 0.380793087982671, 0.68110703188308,
0.0936612801494288, 0.051047767208204, 0.273844437996178, 0.800343046949624,
0.911380903925364, 0.582479041522435, 0.748111458524328, 0.303383302981895,
0.906904782788438, 0.328934737098847, 0.69720659917789, 0.166551542287389,
0.875547286349842, 0.265071728355448, 0.698643970290581, 0.271578241223373,
0.956035715886986, 0.583251175640849, 0.268613096173177, 0.738778731483354,
0.22125582045959, 0.289055369557255, 0.697577213911879), VEG3 = c(0.413110318831049,
0.193370500853792, 0.0443323484313173, 0.312394866761789, 0.381013563679286,
0.774340211752776, 0.315822280223384, 0.499646073445189, 0.0433440470476795,
0.63592018453082, 0.375881818437784, 0.758815519342307, 0.517769074796318,
0.0878879638478763, 0.368268890348083, 0.86661723542387, 0.159257788537873,
0.302684958923527, 0.947570919817041, 0.157392788910852, 0.125769229458094,
0.348650174982873, 0.526963940972057, 0.632646354644645, 0.351109914949622,
0.836552506892888, 0.541982479414601, 0.427697355405521, 0.799356060602721,
0.205934189654569, 0.511986141383475, 0.807482528548695, 0.232665456121877,
0.933325748417277, 0.00683041685826713, 0.21579707708936, 0.92805622685536,
0.754838770281907, 0.39901873988239, 0.991423691630203, 0.73157540991456,
0.572610353705451, 0.806848128394244, 0.445431445389894, 0.26939539811961,
0.584527298222492, 0.386004220382543, 0.242375752770125, 0.558781312238412,
0.026491283834456, 0.343044539065029, 0.802187562912211, 0.885495299962394,
0.853256462542473, 0.126077883381321, 0.780548336023421, 0.840250789403559,
0.724812876802032, 0.704098036072535, 0.201989727535322), VEG4 = c(0.640799340986514,
0.987906611244886, 0.619024206202544, 0.209427026032481, 0.489727139189696,
0.687333141302772, 0.705507906383631, 0.208889941178721, 0.396874200897516,
0.712945929935184, 0.973888093276408, 0.763823354263263, 0.377489995555215,
0.0106930906722855, 0.803664402691951, 0.278756822036515, 0.0550468580770419,
0.265168521344556, 0.987573377094547, 0.734619443919844, 0.732929467499789,
0.241731972053084, 0.559823481667716, 0.155701349465554, 0.478406027901528,
0.961160568832851, 0.0998154836815058, 0.412094044212779, 0.243883425544374,
0.942308621371675, 0.939264039325847, 0.255191997669476, 0.25098970135836,
0.882652992136823, 0.287854570522429, 0.332318399664456, 0.412416503681846,
0.321868386633491, 0.415802982737621, 0.518373582745234, 0.160366074863356,
0.328725259142546, 0.740582698190321, 0.630219424448588, 0.0963326376312392,
0.841597785551852, 0.157610936357946, 0.37344035882236, 0.665277345336568,
0.472350881237637, 0.905421464878138, 0.67001107287601, 0.931361601273204,
0.478463787477743, 0.457093329803116, 0.901963213218536, 0.0989040910780277,
0.648496145325191, 0.149756719258531, 0.766506521363941), VEG5 = c(0.180283594647103,
0.787728519142257, 0.577806490890149, 0.909505719647681, 0.843259853705306,
0.19170485676349, 0.88285289130116, 0.390956223533144, 0.400453739302465,
0.138993973168619, 0.701071911445177, 0.0275528476660395, 0.196129789901458,
0.243754073834767, 0.586994020311301, 0.309287604588564, 0.54116171026274,
0.804029207727762, 0.633322226579327, 0.576488317293166, 0.657529806993353,
0.896942372401784, 0.69552906585738, 0.00605796084044197, 0.0333828607443704,
0.421697067898017, 0.344727073066524, 0.261778629704536, 0.662947164033825,
0.264729400387399, 0.361422182455181, 0.867093056031147, 0.994741928030988,
0.1442062746899, 0.757388857488693, 0.0417225005179914, 0.448764060787373,
0.844033548534798, 0.615968575276885, 0.424045632312236, 0.419379052486891,
0.47584349576189, 0.209702827083009, 0.765825214487151, 0.834675742228314,
0.1887294652982, 0.960231969290817, 0.935011727569393, 0.976674243295263,
0.820848463468705, 0.341388666484707, 0.552464582497579, 0.573838987965132,
0.698330744449092, 0.383783839725491, 0.713851582153272, 0.964603044208545,
0.70783623057194, 0.608167299267236, 0.461582775185318)), class = "data.frame", row.names = c(NA,
-60L))
代码:
library(ggplot2)
library(ggcorrplot)
library(corrplot)
#Generate correlation matrix
res <- cor(sen)
#Generate matrix graphic
ggcorrplot(res, hc.order = FALSE, type = "lower",
outline.col = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", "white", "#E46726"),
lab = TRUE, lab_size = 7, show.diag=FALSE,
legend.title = "r-value", tl.cex=17)+
theme(
legend.key.width=unit(1, "cm"),
legend.key.height=unit(2.5, "cm"),
legend.title=element_text(size=20),
legend.text=element_text(size=15)
)
library(reshape2)
library(ggplot2)
res <- cor(sen)
res[upper.tri(res)] = NA
diag(res) = NA
d = melt(res)
d = d[d$Var2 %in% paste0("STAGE", 1:3),]
graphics.off()
ggplot(d, aes(x = Var1, y = Var2, fill = value, label = round(value, 2))) +
geom_tile(color = "black") +
geom_text() +
labs(fill = "r-value") +
coord_equal() #OPTIONAL
我发现另一种解决方案是将比例调整到可以裁剪掉不需要的变量的位置。这不是最技术性的答案,但它适用于我需要的参数:
png("wholeplant.png", width=900, height=600)
ggcorrplot(res, hc.order = FALSE, type = "lower",
outline.col = "white",
ggtheme = ggplot2::theme_gray,
colors = c("#6D9EC1", "white", "#E46726"),
lab = TRUE, lab_size = 7, show.diag=FALSE,
legend.title = "r-value", tl.cex=17)+
theme(
#resize the legend width and height
legend.key.width=unit(1, "cm"),
legend.key.height=unit(1, "cm"),
legend.title=element_text(size=20),
legend.text=element_text(size=15),
#Put it where it won't be out of the way
legend.position = c(1.1, 0.2)
)
dev.off()