gpt4 book ai didi

r - 添加形状时分组哑铃图变得不稳定

转载 作者:行者123 更新时间:2023-12-01 01:40:16 33 4
gpt4 key购买 nike

我有以下数据显示白人与黑人在各个领域的一致性百分比。我想创建一个分组的哑铃图,其中 National 值和 State 值彼此相邻,以便于比较。

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)

enter image description here

当我的代码如下时,我得到一个图表,其中所有的点都很好地排列:
df <- data.frame(Domain, Area, race, pct_agreement)

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

enter image description here

但是当我试图根据受访者是黑人还是白人来制作不同的形状时,一切都突然变得不稳定。这是一个错误吗?有什么办法可以解决它或解决它吗?

我也不确定如何在“哑铃”的末端之间添加一条线——我尝试使用 geom_line(aes(group=Area)) 但这导致所有哑铃都被连接起来。
ggplot(df) +
geom_point(aes(x=Domain, y=pct_agreement, color=Area, shape=race),
position=position_dodge(width=1)) +
coord_flip()

enter image description here

注意:我浏览了本网站上的许多帖子以试图找到答案,许多帖子都建议使用 facet。这对我不起作用,因为我的老板希望将所有内容都放在一张图表上。

最佳答案

躲避是基于组进行的,并且通过两个分类变量( raceArea ),您最终会为每个域创建四个一组,因此这些点会被躲避到四个不同的位置。您可以通过显式设置组美学来避免这种情况。

先上数据:

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



reprex package (v0.3.0) 于 2019-11-08 创建

用线连接点很困难。我认为它值得一个单独的问题。我发布了一个 here.

关于r - 添加形状时分组哑铃图变得不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58756264/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com