ggplot2:摘要行和摘要的独立连续填充;柱子 [英] ggplot2: Independent Continuous Fill for Summary Row & Column

查看:84
本文介绍了ggplot2:摘要行和摘要的独立连续填充;柱子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种策略,使用geom_tile在热图中绘制摘要(总计)行,这涉及在data_frame中为行和列的总计创建额外的行:

I am using a strategy to plot summary (totals) rows in a heatmap using geom_tile, which involves creating extra rows in the data_frame for row and column totals:

library(dplyr)
library(ggplot2)    

entitites = LETTERS[1:10]
# create some sample data
df_foo = bind_cols(
  data_frame(Group1 = rep(c("A", "B"), each = 100)),
  bind_rows(
    expand.grid(
      Left = entitites, Right = entitites,
      stringsAsFactors = FALSE
    ),
    expand.grid(
      Left = entitites, Right = entitites,
      stringsAsFactors = FALSE
    )
  ),
  data_frame(Value = rpois(200, 15))
)

# create the summary row & column
df_foo_aug = bind_rows(
  df_foo,
  df_foo %>% 
    group_by(Left, Group1) %>% 
    summarize(
      Value = sum(Value),
      Right = "Total"
    ),
  df_foo %>% 
    group_by(Right, Group1) %>% 
    summarize(
      Value = sum(Value),
      Left = "Total"
    )
)

# create the plot
df_foo_aug %>% 
  ggplot(aes(x = Right, y = Left, fill = Value)) + 
  geom_tile() + 
  facet_wrap(~ Group1) + 
  theme_bw()

这将产生:

很显然,总计行/列需要它们自己的填充渐变,但是尚不清楚如何(如果)可以添加第二个连续/渐变填充.

Obviously, the totals row/column need their own fill gradient, but it is not clear how (if) I can add a second continuous/gradient fill.

任何其他达到相同预期结果的方法也可以作为解决该问题的方法.

Any other way to achieve the same intended outcome would be acceptable as a solution to this question as well.

推荐答案

此处的问题是,在ggplot中,原则上美学只能具有一个尺度.所以fill只能有一个标度.但是,有一些方法可以避免这种情况,例如通过将color用于第二刻度.或者,您可以按照

The problem here is that in ggplot, in principle, an aesthetic can only have one scale. So fill can only have one scale. However, there are some ways to avoid this, for example by using color for a second scale. Alternatively, you could mess around with grobs to get the job done, as per shayaa's comment.

以下是一些可能的示例,使用geom_point显示总计:

Here are some possible examples, using geom_point to display the totals:

base_plot <-  
  ggplot(df_foo_aug, aes(x = Right, y = Left)) + 
  geom_tile(data = filter(df_foo_aug, Right != 'Total', Left != 'Total'), 
            aes(fill = Value)) +
  coord_equal() +
  facet_wrap(~ Group1) + 
  scale_y_discrete(limits = rev(sort(unique(df_foo_aug$Left)))) +
  theme_classic() + theme(strip.background = element_blank())

一个相当标准的方法:

base_plot +
  geom_point(data = filter(df_foo_aug, Right == 'Total' | Left == 'Total'), 
             aes(col = Value), size = 9.2, shape = 15) +
  scale_color_gradient('Total', low = 'black', high = 'red')

使用可感知范围更广的色标:

Using color scales with a wider perceptual range:

base_plot +
  geom_point(data = filter(df_foo_aug, Right == 'Total' | Left == 'Total'), 
            aes(col = Value), size = 9.2, shape = 15) +
  viridis::scale_fill_viridis(option = 'B') +
  viridis::scale_color_viridis('Total', option = 'D')

还将size映射到总Value:

base_plot +
  geom_point(data = filter(df_foo_aug, Right == 'Total' | Left == 'Total'), 
             aes(col = Value, size = Value)) +
  scale_size_area(max_size = 8, guide = 'none') +
  viridis::scale_fill_viridis(option = 'B') +
  viridis::scale_color_viridis('Total', option = 'D')

我个人很喜欢最后一个.

Personally, I quite like the last one.

最后一项改进是将y轴向上移动,为此,我建议

One final improvement would be to move the y-axis up, for which I would recommend the cowplot package.

这篇关于ggplot2:摘要行和摘要的独立连续填充;柱子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆