ggplot2躲闪的哑铃情节 [英] Dodged dumbbell plots with ggplot2

查看:83
本文介绍了ggplot2躲闪的哑铃情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题基于此先前的问题.

请考虑以下情节:

Domain = c("A", "B", "C", "D", "E", "F", "G", 
           "A", "B", "C", "D", "E", "F", "G", "A", "B", "C", "D", "E", "F", 
           "G", "A", "B", "C", "D", "E", "F", "G") 

Area = c("State", "State", 
         "State", "State", "State", "State", "State", "National", "National", 
         "National", "National", "National", "National", "National", "State", 
         "State", "State", "State", "State", "State", "State", "National", 
         "National", "National", "National", "National", "National", "National")

race = c("White", "White", "White", "White", "White", "White", 
         "White", "White", "White", "White", "White", "White", "White", 
         "White", "Black", "Black", "Black", "Black", "Black", "Black", 
         "Black", "Black", "Black", "Black", "Black", "Black", "Black", 
         "Black") 

pct_agreement = c(0.557610213756561, 0.735042750835419, 
                  0.567375898361206, 0.633762538433075, 0.64091557264328, 0.750356614589691, 
                  0.564539015293121, 0.651861846446991, 0.697574973106384, 0.653521358966827, 
                  0.713940441608429, 0.680985689163208, 0.751584351062775, 0.642535984516144, 
                  0.488484561443329, 0.581625580787659, 0.456939995288849, 0.580652594566345, 
                  0.630399644374847, 0.711643815040588, 0.347775995731354, 0.627996683120728, 
                  0.668737232685089, 0.610245823860168, 0.690373718738556, 0.705771028995514, 
                  0.738830924034119, 0.550933301448822)

df <- data.frame(Domain, Area, race, pct_agreement)

library(tidyverse)

ggplot(df) +
  geom_point(
    aes(
      x = Domain, y = pct_agreement, color = Area, shape = race,
      group = Area
    ), 
    position = position_dodge(width = 1)
  ) +
  coord_flip()

现在,我们想通过连接相同域和面积的每对点将其转换为哑铃图.显而易见的代码如下:

Now we want to turn it into a dumbbell plot by connecting each pair of points of the same domain and area. The obvious code would be the following:

df2 <- pivot_wider(df, names_from = race, values_from = pct_agreement)

ggplot(df) +
  geom_point(
    aes(
      x = Domain, y = pct_agreement, color = Area, shape = race,
      group = Area
    ), 
    position = position_dodge(width = 1)
  ) +
  geom_segment(
    data = df2,
    aes(
      x = Domain, xend = Domain, y = White, yend = Black,
      color = Area
    ),
    position = position_dodge(width = 1)
  ) +
  coord_flip()

reprex软件包(v0.3.0)于2019-11-08创建 sup>

Created on 2019-11-08 by the reprex package (v0.3.0)

但是,显然,这是行不通的,因为position_dodge()并没有躲过xend美学.我认为这可能是应该修复的ggplot2中的错误.但是,与此同时,制作此图的最简单方法是什么?我可以想到各种方法来做,但是它们似乎都很麻烦.我想念什么吗?

Yet, clearly, that didn't work, because position_dodge() didn't dodge the xend aesthetic. I consider this a bug in ggplot2 that probably should be fixed. However, in the mean time, what's the easiest way of making this plot? I can think of various ways to do it but they all seem cumbersome. Am I missing something?

推荐答案

我发现嵌套的分组通常会导致特定图的困难.在这些情况下,我发现interaction()函数非常有用,因为它允许简洁的代码并避免数据重新格式化.我们可以为线段定义新的分组.

I find nested groupings can often lead to difficulties with specific plots. In these situations I found the interaction() function to be very helpful since it allows for concise code and avoiding the data reformatting. We can define a new grouping for the line segments.

重要的是,以下解决方案适用于位置躲避,因为它使用geom_line()而不是geom_segment().这避免了xend无法处理的xend美学.

Importantly, the following solution works with position dodging because it uses geom_line() instead of geom_segment(). This avoids the xend aesthetic which position_dodge() can't handle.

ggplot(df) +
  geom_point(
    aes(
      x = Domain, y = pct_agreement, color = Area,
      shape = race, group = Area
    ), 
    position = position_dodge(width = 0.5)
  ) +
  geom_line(
    aes(
      x = Domain, y = pct_agreement, color = Area, 
      group = interaction(Area, Domain)
    ),
    position = position_dodge(width = 0.5)
  ) +
  coord_flip()

这篇关于ggplot2躲闪的哑铃情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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