这个问题是基于这个先前的问题。 https://stackoverflow.com/q/58756264/4975218
考虑以下情节:
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()
现在我们想通过连接同一域和面积的每一对点将其变成哑铃图。明显的代码如下:
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()
Created on 2019-11-08 by the reprex package https://reprex.tidyverse.org (v0.3.0)
然而,显然这不起作用,因为position_dodge()
并没有躲避xend
审美的。我认为这是 ggplot2 中的一个错误,可能应该修复。然而,与此同时,制作这个情节最简单的方法是什么?我可以想到各种方法来做到这一点,但它们看起来都很麻烦。我错过了什么吗?