Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The data I am working with is available here, or below:

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

Similar to other users (here, and here), I am trying to plot this data so that the line only goes through specific points (in my case, the black points). Note: the orange (A in Age column) and red (A1 in Age column) points are there to illustrate how my results compare to others.

The closest I've been able to get is:

enter image description here

The code to get this plot is:

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

Is there a way I can have geom_line() connect just the black points?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

as @IceCreamToucan noted, you could accomplish this by filtering to just include. the relevant Age series.

(I am running into an issue where the alignment between the points and lines is not consistent for the last life stage. Not sure how to resolve.)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...