ggplot2: Independent Continuous Fill for Summary Row & Column

后端 未结 1 680
南方客
南方客 2021-01-20 22:47

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

相关标签:
1条回答
  • 2021-01-20 22:58

    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.

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

    A fairly standard approach:

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

    Also mapping size to the total 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.

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

    0 讨论(0)
提交回复
热议问题