library(ggplot2)
#install.packages("devtools")
#devtools::install_github("junjunlab/jjPlot")
library(jjPlot)
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.4.1
##
## Attaching package: 'reshape2'
##
## The following object is masked from 'package:tidyr':
##
## smiths
library(ggnewscale)
library(cowplot)
## Warning: package 'cowplot' was built under R version 4.4.1
##
## Attaching package: 'cowplot'
##
## The following object is masked from 'package:lubridate':
##
## stamp
# test
cor_data <- cor(mtcars) %>%
data.frame() %>%
mutate(x = rownames(.)) %>%
melt(.,id.vars = "x")
cor_data <- cor_data %>% filter(variable %in% c('mpg','cyl','disp','hp'))
dim(cor_data)
## [1] 44 3
#cor_data
# add some value
cor_data$value2 <- rev(cor_data$value)
cor_data$value3 <- runif(44,min = -1,max = 1)
cor_data$value4 <- runif(44,min = -1,max = 1)
################################################7.1 right triangle
# basic plot
cor_data
## x variable value value2 value3 value4
## 1 mpg mpg 1.0000000 0.7498125 0.49951277 -0.23261508
## 2 cyl mpg -0.8521620 -0.1257043 0.68490788 0.62930856
## 3 disp mpg -0.8475514 -0.2432043 0.57499096 -0.83006383
## 4 hp mpg -0.7761684 -0.7230967 -0.12839473 0.34921286
## 5 drat mpg 0.6811719 -0.7082234 0.23036845 -0.61054217
## 6 wt mpg -0.8676594 0.6587479 -0.58212653 0.10828929
## 7 qsec mpg 0.4186840 -0.4487591 0.27843972 -0.30586671
## 8 vs mpg 0.6640389 1.0000000 0.10515065 0.46033858
## 9 am mpg 0.5998324 0.7909486 0.14137552 -0.85601939
## 10 gear mpg 0.4802848 0.8324475 0.45881096 -0.50403006
## 11 carb mpg -0.5509251 -0.7761684 -0.42636706 -0.69212840
## 12 mpg cyl -0.8521620 0.3949769 0.66715237 -0.88367395
## 13 cyl cyl 1.0000000 -0.5555692 0.46019306 0.53115182
## 14 disp cyl 0.9020329 -0.5912270 0.16169117 -0.41275143
## 15 hp cyl 0.8324475 -0.7104159 0.51052459 -0.44786184
## 16 drat cyl -0.6999381 -0.4336979 0.99234154 -0.30696995
## 17 wt cyl 0.7824958 0.8879799 -0.82253424 -0.30026096
## 18 qsec cyl -0.5912421 -0.7102139 -0.12932624 -0.20555526
## 19 vs cyl -0.8108118 0.7909486 0.93117796 0.83642637
## 20 am cyl -0.5226070 1.0000000 0.94198362 -0.10638701
## 21 gear cyl -0.4926866 0.9020329 0.62266217 -0.99251744
## 22 carb cyl 0.5269883 -0.8475514 -0.87799679 -0.52103505
## 23 mpg disp -0.8475514 0.5269883 0.58147596 -0.72210885
## 24 cyl disp 0.9020329 -0.4926866 -0.03099023 -0.39208995
## 25 disp disp 1.0000000 -0.5226070 -0.05062154 -0.46162129
## 26 hp disp 0.7909486 -0.8108118 -0.48763843 -0.93092867
## 27 drat disp -0.7102139 -0.5912421 -0.79413678 -0.95351881
## 28 wt disp 0.8879799 0.7824958 -0.80901436 -0.31245750
## 29 qsec disp -0.4336979 -0.6999381 0.77537727 0.07699345
## 30 vs disp -0.7104159 0.8324475 0.87748256 -0.64048883
## 31 am disp -0.5912270 0.9020329 -0.28767760 -0.20422148
## 32 gear disp -0.5555692 1.0000000 -0.18031490 -0.05423099
## 33 carb disp 0.3949769 -0.8521620 -0.95393926 -0.91896125
## 34 mpg hp -0.7761684 -0.5509251 -0.30459905 0.94076288
## 35 cyl hp 0.8324475 0.4802848 0.16248875 -0.56475282
## 36 disp hp 0.7909486 0.5998324 0.88253614 0.28793416
## 37 hp hp 1.0000000 0.6640389 0.59664349 0.43218514
## 38 drat hp -0.4487591 0.4186840 0.04979152 -0.01630703
## 39 wt hp 0.6587479 -0.8676594 0.36232902 0.22478365
## 40 qsec hp -0.7082234 0.6811719 0.22136747 0.23733070
## 41 vs hp -0.7230967 -0.7761684 -0.49251894 0.45267485
## 42 am hp -0.2432043 -0.8475514 -0.24963059 0.41779658
## 43 gear hp -0.1257043 -0.8521620 -0.17629684 0.79904196
## 44 carb hp 0.7498125 1.0000000 0.67834907 0.27122072
pb <-
ggplot(cor_data,
aes(x = x,y = variable,fill = value)) +
coord_fixed()
# right triangle
pul <- pb + geom_jjtriangle(type = 'ul')
pbl <- pb + geom_jjtriangle(type = 'bl')
pur <- pb + geom_jjtriangle(type = 'ur')
pbr <- pb + geom_jjtriangle(type = 'br')
# combine
cowplot::plot_grid(plotlist = list(pul,pbl,pur,pbr),
nrow = 2,align = 'hv')

############################################7.2 isosceles triangle
# isosceles triangle
pmu <- pb + geom_jjtriangle(type = 'mu')
pmb <- pb + geom_jjtriangle(type = 'mb')
pml <- pb + geom_jjtriangle(type = 'ml')
pmr <- pb + geom_jjtriangle(type = 'mr')
# combine
cowplot::plot_grid(plotlist = list(pmu,pmb,pml,pmr),
nrow = 2,align = 'hv')

# centri triangle
pb + geom_jjtriangle(type = 'centri',width = 0.6) +
scale_fill_gradient(low = 'grey80',high = '#009933')

###########################################7.3 rect graph
# rect
pup <- pb + geom_jjtriangle(rect = T,type = 'upper')
pbt <- pb + geom_jjtriangle(rect = T,type = 'bottom')
# combine
cowplot::plot_grid(plotlist = list(pup,pbt),
nrow = 2,align = 'hv')

##########################################7.4 examples
# exmaple 1
head(cor_data)
## x variable value value2 value3 value4
## 1 mpg mpg 1.0000000 0.7498125 0.4995128 -0.2326151
## 2 cyl mpg -0.8521620 -0.1257043 0.6849079 0.6293086
## 3 disp mpg -0.8475514 -0.2432043 0.5749910 -0.8300638
## 4 hp mpg -0.7761684 -0.7230967 -0.1283947 0.3492129
## 5 drat mpg 0.6811719 -0.7082234 0.2303685 -0.6105422
## 6 wt mpg -0.8676594 0.6587479 -0.5821265 0.1082893
ggplot(cor_data,
aes(x = x,y = variable)) +
geom_jjtriangle(aes(fill = value),type = 'ul') +
scale_fill_gradient(low = '#FFCC33',high = '#99CC33') +
# new legend
new_scale_fill() +
geom_jjtriangle(aes(fill = value2),type = 'br') +
scale_fill_gradient(low = '#66CCCC',high = '#FF3399') +
coord_fixed()

# exmaple 2
ggplot(cor_data,
aes(x = x,y = variable)) +
geom_jjtriangle(aes(fill = value),type = 'mu') +
scale_fill_gradient(low = '#FFCC33',high = '#99CC33') +
# new legend
new_scale_fill() +
geom_jjtriangle(aes(fill = value2),type = 'mb') +
scale_fill_gradient(low = '#66CCCC',high = '#FF3399') +
# new legend
new_scale_fill() +
geom_jjtriangle(aes(fill = value3),type = 'ml') +
scale_fill_gradient(low = '#FF9966',high = '#FF6666') +
# new legend
new_scale_fill() +
geom_jjtriangle(aes(fill = value4),type = 'mr') +
scale_fill_gradient(low = '#0099CC',high = '#666666') +
theme(legend.position = 'top') +
coord_fixed()

# change width
ggplot(cor_data,
aes(x = x,y = variable)) +
geom_jjtriangle(aes(fill = value),type = 'mu') +
scale_fill_gradient(low = '#FFCC33',high = '#99CC33') +
# new legend
new_scale_fill() +
geom_jjtriangle(aes(fill = value2),type = 'mb') +
scale_fill_gradient(low = '#66CCCC',high = '#FF3399') +
# new legend
new_scale_fill() +
geom_jjtriangle(aes(fill = value3),type = 'ml',width = 0.5) +
scale_fill_gradient(low = '#FF9966',high = '#FF6666') +
# new legend
new_scale_fill() +
geom_jjtriangle(aes(fill = value4),type = 'mr',width = 0.5) +
scale_fill_gradient(low = '#0099CC',high = '#666666') +
theme(legend.position = 'top') +
coord_fixed()

# exmaple 3
ggplot(cor_data,
aes(x = x,y = variable)) +
geom_jjtriangle(aes(fill = value),
rect = T,
width = 0.8,
type = 'upper') +
scale_fill_gradient(low = '#FFCC33',high = '#99CC33') +
# new legend
new_scale_fill() +
geom_jjtriangle(aes(fill = value2),
rect = T,
width = 0.8,
type = 'bottom') +
scale_fill_gradient(low = '#66CCCC',high = '#FF3399') +
coord_fixed()

#ref https://mp.weixin.qq.com/s/ZoTtt1wZQDLc5e5_MBHutQ
#ref https://junjunlab.github.io/jjPlot-manual/intro.html#example