前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggasym轻松给热图添加多个图例

ggasym轻松给热图添加多个图例

作者头像
R语言数据分析指南
发布2023-08-18 13:37:55
1730
发布2023-08-18 13:37:55
举报

欢迎关注R语言数据分析指南

❝本节了介绍一款R包「ggasym」其主要作用为绘制热图时根据数据添加多个连续型图例,以往遇到热图需要多个图例的问题小编都是分批绘制,使用「ggasym」可以看到代码过程明显有所简化,感兴趣的朋友欢迎分享转发,「更多详细内容请参考作者官方文档」

官方文档

❝https://jhrcook.github.io/ggasym/index.html ❞

加载R包

代码语言:javascript
复制
devtools::install_github("jhrcook/ggasym")
# install.packages("ggasym")

library(tidyverse)
library(ggasym)

构建数据

代码语言:javascript
复制
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)
)

案例 1

代码语言:javascript
复制
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()

案例 2

代码语言:javascript
复制
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))

案例 3

代码语言:javascript
复制
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"
    )
  )

案例4 分面操作

代码语言:javascript
复制
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)
代码语言:javascript
复制
tib <- ggplot2::txhousing %>%
  filter(city == "Austin") %>%
  mutate(year = as.character(year))
aov_res <- aov(median ~ year, data = tib)
broom::tidy(aov_res)

案例 5

代码语言:javascript
复制
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))
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-06-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言数据分析指南 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 欢迎关注R语言数据分析指南
  • 官方文档
  • 加载R包
  • 构建数据
  • 案例 1
  • 案例 2
  • 案例 3
  • 案例4 分面操作
  • 案例 5
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档