Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

r - geom_line to only connect some points

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...