Chapter 7 geom_jjtriangle
geom_jjtriangle helps you to visualize multiple variables in multiple triangle graph parts.
first we prepare test data:
library(ggplot2)
library(jjPlot)
library(reshape2)
library(ggnewscale)
# test
<- cor(mtcars) %>%
cor_data data.frame() %>%
mutate(x = rownames(.)) %>%
melt(.,id.vars = "x")
<- cor_data %>% filter(variable %in% c('mpg','cyl','disp','hp'))
cor_data
# add some value
$value2 <- rev(cor_data$value)
cor_data$value3 <- runif(44,min = -1,max = 1)
cor_data$value4 <- runif(44,min = -1,max = 1) cor_data
7.1 right triangle
there are four types or right triangle can be ploted with giving a specified type:
# basic plot
<-
pb ggplot(cor_data,
aes(x = x,y = variable,fill = value)) +
coord_fixed()
# right triangle
<- pb + geom_jjtriangle(type = 'ul')
pul <- pb + geom_jjtriangle(type = 'bl')
pbl <- pb + geom_jjtriangle(type = 'ur')
pur <- pb + geom_jjtriangle(type = 'br')
pbr
# combine
::plot_grid(plotlist = list(pul,pbl,pur,pbr),
cowplotnrow = 2,align = 'hv')
7.2 isosceles triangle
there are also four types or isosceles triangle can be ploted with giving a specified type:
# isosceles triangle
<- pb + geom_jjtriangle(type = 'mu')
pmu <- pb + geom_jjtriangle(type = 'mb')
pmb <- pb + geom_jjtriangle(type = 'ml')
pml <- pb + geom_jjtriangle(type = 'mr')
pmr
# combine
::plot_grid(plotlist = list(pmu,pmb,pml,pmr),
cowplotnrow = 2,align = 'hv')
centri type, you can put the triangle in the middle:
# centri triangle
+ geom_jjtriangle(type = 'centri',width = 0.6) +
pb scale_fill_gradient(low = 'grey80',high = '#009933')
7.3 rect graph
there are two types of rect:
# rect
<- pb + geom_jjtriangle(rect = T,type = 'upper')
pup <- pb + geom_jjtriangle(rect = T,type = 'bottom')
pbt
# combine
::plot_grid(plotlist = list(pup,pbt),
cowplotnrow = 2,align = 'hv')
7.4 examples
here we show some examples.
exmaple 1:
# exmaple 1
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:
# 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:
# 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:
# 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()