ggplot2:摘要行的独立连续填充&am​​p;柱

时间:2016-08-24 07:20:50

标签: r ggplot2

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

data_frame

这会产生:

enter image description here

显然,总行/列需要自己的填充渐变,但不清楚如何(如果)我可以添加第二个连续/渐变填充。

实现相同预期结果的任何其他方式也可以作为此问题的解决方案。

1 个答案:

答案 0 :(得分:3)

这里的问题是,在ggplot中,原则上,审美只能有一个尺度。所以fill只能有一个比例。但是,有一些方法可以避免这种情况,例如使用color进行第二次缩放。或者,根据shayaa's comment,您可以使用grobs来完成工作。

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

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')

enter image description here

使用具有更宽感知范围的色标:

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')

enter image description here

同时将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')

enter image description here

就个人而言,我非常喜欢最后一个。

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