3

我希望在 R 中绘制类似的东西。这可以用 ggplot 或其他包来完成吗?

轨迹树

在以下博客中找到:

http://intelligenttradingtech.blogspot.com/2011/07/pattern-recognition-forward-boxplot.html

4

2 回答 2

3

以下是如何使用ggplot2.

在此处输入图像描述

我通过指定每条线的开始和结束位置的坐标来手动构建数据。一个改进显然是使用算法自动执行此操作。由于这不是问题,我也没有尝试解决这个问题。

创建数据:

arrowdata <- c(
  0, 0, 1, 0,
  1, 1, 2, 1,
  1, -1, 2, -1,
  2, 1.5, 3, 1.5,
  2, 0.5, 3, 0.5
)

linesdata <- c(
  1, 0, 1, 1,
  1, 0, 1, -1,
  2, 1, 2, 1.5,
  2, 1, 2, 0.5
)

labeldata <- data.frame(
  x = c(0.5, 1.5, 2.5),
  y = c(0, 1, 1.5),
  labels=c("Label 1", "Label2", "Label 3")
)

adat <- as.data.frame(matrix(arrowdata, ncol=4, byrow=TRUE))
ldat <- as.data.frame(matrix(linesdata, ncol=4, byrow=TRUE))

加载ggplot2grid包,然后绘制:

library(ggplot2)
library(grid) # For arrow() function
ggplot() + 
  geom_segment(
    data=adat, 
    aes(x=V1, y=V2, xend=V3, yend=V4),
    arrow=arrow(length = unit(0.05, "npc"), type="closed"),
    col="blue"
  ) +
  geom_segment(
    data=ldat, 
    aes(x=V1, y=V2, xend=V3, yend=V4),
    col="blue"
  ) +
  geom_text(data=labeldata, aes(x, y, label=labels), 
    size=8, vjust=-0.2, col="blue"
  ) +
  theme_bw() +
  opts(
    axis.text.x=theme_blank(),
    axis.text.y=theme_blank(),
    axis.ticks=theme_blank(),
    axis.title.x=theme_blank(),
    axis.title.y=theme_blank(),
    panel.grid.major=theme_blank(),
    panel.border=theme_blank()
  ) +
coord_cartesian(ylim=c(-1.5, 2)) # Create some additional space for labels
于 2012-05-23T08:52:23.290 回答
1

您可能会在http://addictedtor.free.fr/graphiques/找到一些东西。那里有各种各样的图形和图表。plot现在,使用将在顶点之间绘制线条的基础和graphics::arrow函数编写一些代码已经很容易了。例如,

arrows(0,1,0,0) 
lines(c(1,1),c(-.5,.5)) 
arrows(1,2,.5,.5) 

等等。您是否需要根据数据来调整或放置分支的大小,或者这是一个纯粹的定性树?

于 2012-05-17T11:23:16.310 回答