geom_line只连接一些点

时间:2019-02-16 22:06:20

标签: r if-statement ggplot2

我正在使用的数据here或以下版本可用:

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  A1     D
Activity       0.4229638 0.4561744 0.3854906  CA     D
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  A1     D
Aggression     0.3212115 0.3471766 0.2801818  CA     D
Aggression     0.5106432 0.5635857 0.4634950  CY     C

类似于其他用户(herehere),我试图绘制此数据,以便该线仅经过特定点(在我的情况下为黑点)。注意:橙色(A列中的Age和红色(A1列中的Age)点是用来说明我的结果与其他结果的比较。

我能得到的最接近的是:

enter image description here

获得该图的代码是:

pd <- position_dodge(0.3)

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))+
  scale_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)")) +
  scale_colour_manual(values = c("orange", "red", "black", "black", "black", "black"), name = "Study", breaks=c("A","A1","PE","PW", "CA", "CY")) + 
  guides(colour=FALSE)+ 
  geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+ theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        plot.caption = element_text(hjust = 0, vjust = 2.12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        text = element_text(size = 15)) +
  labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")

有没有办法让geom_line()仅连接黑点?

2 个答案:

答案 0 :(得分:1)

正如@IceCreamToucan指出的那样,您可以通过过滤以包括在内来完成此操作。相关的年龄序列。

(我遇到的问题是,在上一生命阶段,点和线之间的对齐方式不一致。不确定如何解决。)

library(tidyverse)
ggplot(data = rep, aes(x = stage, y = Repeatability)) +
  geom_point(aes(shape = Behaviour, colour=Age),
             position = position_dodge(width = 0.3), size = 3) +
  geom_line(data = rep %>% filter(Age %in% c("PE", "PW", "CA", "CY")),
            aes(group = Behaviour), 
            position = position_dodge(width = 0.3)) +

  # as in OP....
  scale_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)")) +
  scale_colour_manual(values = c("orange", "red", "black", "black", "black", "black"), name = "Study", breaks=c("A","A1","PE","PW", "CA", "CY"))  +
  guides(colour=FALSE)+
  # excluded because data missing from question text
  # geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+ 
  theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        plot.caption = element_text(hjust = 0, vjust = 2.12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        text = element_text(size = 15)) +
  labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")

enter image description here

答案 1 :(得分:0)

如果其他人遇到类似问题,我将发布最终对我有用的解决方案。

我能够通过首先重命名Age列中的变量以更改其顺序来获得所需的图。将CA更改为A1,将A1更改为A2

然后,如@IceCreamToucan和@Jon Spring建议的那样:

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

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_x_discrete(labels=c("A" = "50-70 days old", "B" = "70-365 days old", "C" = "Yearlings (365 days old)", "D" = "Adults (>365 days old)"))) + 
    guides(colour=FALSE)+ #removes the legend showing colour
    geom_errorbar(aes(ymin=LCI, ymax=UCI), position=pd, width=0.1, size=0.5)+     
    theme_bw() +
    theme(axis.line = element_line(colour = "black"),
        plot.caption = element_text(hjust = 0, vjust = 2.12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        text = element_text(size = 15)) +
  labs(y = "Repeatability (r) ± 95 % CrI", x = "Life stage")

然后给出所需的绘图:

enter image description here