R 使用带几何线的位置减淡

R 使用带几何线的位置减淡,r,ggplot2,R,Ggplot2,我的问题与此密切相关,但这是一个后续问题,不是重复问题。我的绘图问题与使用position\u dodge()和geom\u 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

我的问题与此密切相关,但这是一个后续问题,不是重复问题。我的绘图问题与使用
position\u dodge()
geom\u 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)

pd只需将
group=behavior
移动到
ggplot(…,aes(…,group=behavior))


好的,这是另一个选择。其思想是使用
抖动
预先计算闪避位置。这将把分类变量
stage
转换为连续变量
stage.jitter
,需要通过
scale\x\u 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)


您可能需要通过更改
jitter
中的
因子
值来调整抖动量,这是一个小细节,但我注意到,在您的绘图中,不同颜色的点(带有SE条)都彼此对齐,而在我上面的绘图中,它们都偏移以便于查看。有没有办法让线穿过黑点,同时也保持点的
位置\u dodge()
?如果无法偏移点以便于可视化,有没有办法将彩色点移到前面(并将黑点移到后面)?Hi@BlunderingEcologist;我明白你的意思,但我不确定有没有一个简单的解决办法。我们需要
美学来画线(因为
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)