Соедините несколько временных точек с течением времени с помощью geom_segment () в ggplot2 - PullRequest
0 голосов
/ 14 июля 2020

У меня есть data.frame, который выглядит так

tree=data.frame(time=c(0,0,0,1,1,1,2,2,2), pop.size=c(10,10,10,14,13,10,17,24,13), 
                parents=c(NA,NA,NA,1,2,3,1,2,3), offspring=c(1,2,3,1,2,4,5,6,7))

Я хотел бы в каждый момент времени отображать потомство в виде точек с помощью geom_point и соединять их с их родителями в предыдущий момент времени с помощью geom_segment () или любой другой geom, который может оказаться полезным.

ggplot(tree,aes(x=time, y=offspring))+
  geom_point() +
  theme_bw()

enter image description here

Any help would be appreciated. While I can connect the points of time point 1 with time point 2, its hard to do connect the time point 2 with 3. I have to tell you that I have up to 100 points

EDIT: I would like to see something like this. Here I connect only elements of time point 1 with the elements of time point 2. I would like to connect time point 2 with time point 3 and my plot look like a genealogical tree

ggplot(tree,aes(x=time, y=offspring))+
  geom_point() +
  theme_bw() +
  geom_segment(aes(x=time[1], xend=time[5], y=parents, yend=offspring))

введите описание изображения здесь

1 Ответ

0 голосов
/ 14 июля 2020

Вы можете попробовать:

ggplot(tree,aes(x=time, y=offspring,group=time))+geom_line()+
    geom_point() +
    theme_bw()

enter image description here

Or:

ggplot(tree,aes(x=time, y=offspring,group=1))+geom_line()+
    geom_point() +
    theme_bw()

enter image description here

Update

Try this code:

tree %>% group_by(time) %>% mutate(ind = 1:length(time)) -> tree2
ggplot(tree2,aes(x=time, y=offspring,group=ind))+geom_line()+
  geom_point() +
  theme_bw()

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...