gpt4 book ai didi

r - 将 position_dodge 与 geom_line 一起使用

转载 作者:行者123 更新时间:2023-12-04 18:20:25 24 4
gpt4 key购买 nike

我的问题与这个 question 密切相关,但是是一个后续问题,而不是重复。我的绘图问题与将 postion_dodge()geom_line() 一起使用有关。

数据:

Behaviour Repeatability       UCI       LCI Age stage
Activity 0.1890000 0.2470000 0.1600000 PE A
Activity 0.5500000 0.7100000 0.3900000 PW B
Activity 0.5100000 0.6300000 0.4000000 A D
Activity 0.4100000 NA NA A2 D #NAs are real & important
Activity 0.4229638 0.4561744 0.3854906 A1 D
Activity 0.4660000 0.5910000 0.2320000 PW2 B
Activity 0.1812492 0.2111999 0.1522250 CY C
Aggression 0.2620000 0.3030000 0.1960000 PE A
Aggression 0.3700000 0.3800000 0.3600000 PW B
Aggression 0.4400000 0.5600000 0.3300000 A D
Aggression 0.3740000 NA NA A2 D #NAs are real & important
Aggression 0.3212115 0.3471766 0.2801818 A1 D
Aggression 0.0461000 0.0995000 0.0158000 PW2 B
Aggression 0.5106432 0.5635857 0.4634950 CY C

只有相关的 ggplot 代码:
pd <- position_dodge(0.3)

my_colors <-
tibble(color = c("orange", "black", "red", "black", "black", "pink", "black"),
Age = c("A","A1","A2", "CY", "PE","PW", "PW2"))

ggplot(rep, aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age)) +
geom_point(
position = position_dodge(width = 0.3),
size = 3) +
geom_line(
aes(group=Behaviour),
position = position_dodge(width = 0.3),
data = function(x) inner_join(x, my_colors %>% filter(color == 'black')))+
scale_colour_manual(
values = c("orange", "black", "red", "black", "black", "pink", "black"),
name = "Study",
breaks=c("A","A1","A2", "CY", "PE","PW", "PW2"))+
geom_errorbar(
aes(ymin=LCI, ymax=UCI),
position=pd,
width=0.1,
size=0.5)

这是我得到的情节:

enter image description here

如何移动 geom_line() 使其穿过黑点? (而不是在黑色和粉红色点之间。)

我在 position = position_dodge(width = 0.3)geom_point() 代码中都包含了 geom_line() 参数,但它不适用于 geom_line()

最佳答案

刚搬家group = Behaviourggplot(..., aes(..., group = Behaviour)) .

ggplot(rep, aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age, group = Behaviour)) +
geom_point(
position = position_dodge(width = 0.3),
size = 3) +
geom_line(
position = position_dodge(width = 0.3),
data = function(x) inner_join(x, my_colors %>% filter(color == 'black')))+
scale_colour_manual(
values = c("orange", "black", "red", "black", "black", "pink", "black"),
name = "Study",
breaks=c("A","A1","A2", "CY", "PE","PW", "PW2"))+
geom_errorbar(
aes(ymin=LCI, ymax=UCI),
position=pd,
width=0.1,
size=0.5)

Plot

好的,这是另一种选择。这个想法是使用 jitter 预先计算躲避的位置.这将使分类变量 stage变成连续变量 stage.jitter这需要通过 scale_x_continuous 手动指定 x 轴标签.
rep %>%
mutate(stage.jitter = jitter(as.numeric(stage), 0.5)) %>%
ggplot(aes(x = stage.jitter, y = Repeatability, shape = Behaviour, colour=Age, group = Behaviour)) +
geom_point(size = 3) +
geom_line(
data = function(x) inner_join(x, my_colors %>% filter(color == 'black')))+
scale_colour_manual(
values = c("orange", "black", "red", "black", "black", "pink", "black"),
name = "Study",
breaks=c("A","A1","A2", "CY", "PE","PW", "PW2")) +
scale_x_continuous(
"stage",
labels = function(x) rep %>% pull(stage) %>% levels() %>% .[x]) +
geom_errorbar(
aes(ymin = LCI, ymax = UCI),
width = 0.1,
size = 0.5)

Second plot

您可能需要通过更改 factor 来调整抖动量。内部值 jitter .

关于r - 将 position_dodge 与 geom_line 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54775878/

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