❝本节了介绍一款R包「ggasym」其主要作用为绘制热图时根据数据添加多个连续型图例,以往遇到热图需要多个图例的问题小编都是分批绘制,使用「ggasym」可以看到代码过程明显有所简化,感兴趣的朋友欢迎分享转发,「更多详细内容请参考作者官方文档」 ❞
❝https://jhrcook.github.io/ggasym/index.html ❞
devtools::install_github("jhrcook/ggasym")
# install.packages("ggasym")
library(tidyverse)
library(ggasym)
tib <- tibble(
g1 = c("A", "A", "A", "A", "B", "B", "B", "C", "C", "D"),
g2 = c("B", "C", "D", "E", "C", "D", "E", "D", "E", "E"),
val_1 = seq(1, 10, 1),
val_2 = rnorm(10, mean = 0, sd = 3)
)
tib <- asymmetrise(tib, g1, g2)
tib$val_3 <- runif(nrow(tib))
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(fill_tl = val_1, fill_br = val_2, fill_diag = val_3)) +
scale_fill_tl_gradient(low = "lightpink", high = "tomato") +
scale_fill_br_gradient(low = "lightblue1", high = "dodgerblue") +
scale_fill_diag_gradient(low = "yellow", high = "orange3")+
theme_bw()
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(fill_tl = val_1, fill_br = val_2, fill_diag = val_3)) +
scale_fill_tl_gradient(low = "lightpink", high = "tomato") +
scale_fill_br_gradient2(low = "orange", mid = "white", high = "dodgerblue") +
scale_fill_diag_gradientn(colors = rainbow(25))
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(fill_tl = val_1, fill_br = val_2, fill_diag = val_3)) +
scale_fill_tl_gradient(
low = "lightpink", high = "tomato",
guide = guide_colourbar(
direction = "horizontal",
order = 1,
title.position = "top"
)
) +
scale_fill_br_gradient2(
low = "orange", mid = "white", high = "dodgerblue",
guide = guide_colourbar(
direction = "horizontal",
order = 3,
title.position = "top"
)
) +
scale_fill_diag_gradientn(
colors = rainbow(25),
guide = guide_colourbar(
direction = "horizontal",
order = 2,
title.position = "top"
)
)
tib <- tibble(
g1 = rep(c("A", "A", "B"), 2),
g2 = rep(c("B", "C", "C"), 2),
val_1 = seq(1, 6),
val_2 = rnorm(6),
grps = c(1, 1, 1, 2, 2, 2))
tib <- tib %>%
group_by(grps) %>%
asymmetrise(g1, g2) %>%
ungroup()
tib <- tib %>% mutate(val_3 = ifelse(g1 == g2, runif(nrow(tib)), NA))
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(
fill_tl = log(val_1),
fill_br = val_2,
fill_diag = val_3
)) +
scale_fill_tl_gradient(
low = "lightpink", high = "tomato",
guide = guide_colourbar(
direction = "horizontal",
order = 1,
title.position = "top"
)
) +
scale_fill_br_gradient(
low = "lightblue1", high = "dodgerblue",
guide = guide_colourbar(
direction = "horizontal",
order = 3,
title.position = "top"
)
) +
scale_fill_diag_gradient(
low = "grey80", high = "grey20",
guide = guide_colourbar(
direction = "horizontal",
order = 2,
title.position = "top"
)
) +
labs(
fill_tl = "top-left fill",
fill_br = "bottom-right fill",
fill_diag = "diagonal fill",
title = "Example of faceting with ggasym"
) +
theme_bw() +
theme(
axis.title = element_blank(),
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "grey70"),
panel.grid = element_blank()
) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
facet_grid(. ~ grps)
tib <- ggplot2::txhousing %>%
filter(city == "Austin") %>%
mutate(year = as.character(year))
aov_res <- aov(median ~ year, data = tib)
broom::tidy(aov_res)
asymmat_tib <- asymmetrise_stats(TukeyHSD(aov_res))
ggplot(asymmat_tib, aes(x = x, y = y)) +
geom_asymmat(aes(
fill_tl = estimate,
fill_br = -log10(adj.p.value + 0.0000001)
)) +
scale_fill_tl_gradient2(low = "dodgerblue", high = "tomato") +
scale_fill_br_distiller(type = "seq", palette = "Greens", direction = 1) +
labs(
title = "Median House Prices in Austin",
fill_tl = "diff. in\nmean values",
fill_br = "-log10( adj. p-value )"
) +
theme(
panel.background = element_rect(fill = "grey75"),
panel.grid = element_blank()
) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))