具有发展轨迹的散点图 [英] Scatterplot with developmental trajectories

查看:153
本文介绍了具有发展轨迹的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下面的图中绘制发展轨迹,即2009年和2010年的点应该用箭头连接(指向2010年),2010年和2011年的点应该用箭头连接(指着2011年)等等......



这应该适用于这两个组。



这是我到目前为止所做的:

  library(ggplot2 (100,200,300,200,500,320,300,50)
y <-c(100,250,600,700,60,120,
$ bx< -c(100,200,300,200,500,320,300,50) ,200,360)
t < - rep(seq(2009,2012),2)
z < - rep(c(A,B),each = 4)
$ bd < - as.data.frame(cbind(z,t,x,y))
d < - d [order(d $ z,d $ t),]

ggplot(data = d,aes(x = x,y = y,color = z,label = t))+
geom_line(aes(group = z))+
geom_point )+
geom_text()

解决方案

我必须添加一个额外的列 z2 来获取 ggplot2 来将每行看作一个单独的段。否则,只有在每组分段的末尾绘制一个箭头。代码解释最好:

  library(grid)
library(ggplot2)

x< ; c(100,200,300,200,500,320,300,50)
y< -c(100,250,600,700,60,120,200,360)
t< ; - rep(seq(2009,2012),2)
z < - rep(c(A,B),each = 4)
z2 < - 字母[1:长度(z)]

d < - as.data.frame(cbind(z,z2,t,x,y))
d < - d [order(d $ z,d (数据= d,aes(x = x,y = y,color = z2,label = t))+
geom_path(aes(group = z,$ t),]

ggplot ),arrow = arrow(ends =last))+
geom_point()+
geom_text()

...和结果

z2 使用不同的审美元素。 $ b

I want to draw developmental trajectories in the plot below, i.e. the points 2009 and 2010 should be connected by a line with an arrow (pointing at 2010), the points 2010 and 2011 should be connected by a line with an arrow (pointing at 2011) and so on ...

This should apply to both groups.

Here is what I did so far:

library(ggplot2)

x <- c(100, 200, 300, 200, 500, 320, 300, 50)
y <- c(100, 250, 600, 700, 60, 120, 200, 360)
t <- rep(seq(2009,2012),2)
z <- rep(c("A","B"),each=4)

d <- as.data.frame(cbind(z,t,x,y))
d <- d[order(d$z, d$t),]

ggplot(data = d, aes(x = x, y = y, colour = z, label=t)) + 
  geom_line(aes(group = z)) +
  geom_point() +
  geom_text()

My 2 issues are: (1) the "wrong" points are connected and (2) the arrow heads are missing.

解决方案

I had to add an additional column z2 to get ggplot2 to see each line as a separate segment. Otherwise, only at the end of each group of segments an arrowhead was drawn. The code explains best:

library(grid)
library(ggplot2)

x <- c(100, 200, 300, 200, 500, 320, 300, 50)
y <- c(100, 250, 600, 700, 60, 120, 200, 360)
t <- rep(seq(2009,2012),2)
z <- rep(c("A","B"),each=4)
z2 <- LETTERS[1:length(z)]

d <- as.data.frame(cbind(z,z2,t,x,y))
d <- d[order(d$z, d$t),]

ggplot(data = d, aes(x = x, y = y, colour = z2, label=t)) + 
  geom_path(aes(group = z), arrow = arrow(ends = "last")) +
  geom_point() +
  geom_text()

...and the result

Now you only need to tweak the colorscale a bit, or use a different aesthetic for z2.

这篇关于具有发展轨迹的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆