向三角形中心弯曲线(ggplot2)

时间:2013-09-12 01:22:17

标签: r plot ggplot2

我有3个顶点表示为连接三条边的图中的点。我想将边缘弯曲到三角形的中心(c(.5, .35))。如何在ggplot2中将图形1转换为图形2(我假设这个答案也可以推广到基础?虽然顶点保持稳定,但曲线边缘有一些理想的抖动。我认为这意味着某种线性变换,有一些稍微随机的常数。

图1

enter image description here

图2 (颜色仅用于突出显示所需的输出)

enter image description here

library(ggplot2); library(scales)

## The vertices/points data
  point    x    y
1     A 0.25 0.45
2     B 0.50 0.25
3     C 0.75 0.45

## The edges data
  out.x out.y receiver.x receiver.y
1  0.25  0.45       0.50       0.25
2  0.50  0.25       0.75       0.45
3  0.75  0.45       0.25       0.45
4  0.25  0.45       0.50       0.25
5  0.50  0.25       0.75       0.45
6  0.75  0.45       0.25       0.45

## Edges and vertices/points data in dput form for ease
so <- structure(list(out.x = c(0.25, 0.5, 0.75, 0.25, 0.5, 0.75), out.y = c(0.45, 
    0.25, 0.45, 0.45, 0.25, 0.45), receiver.x = c(0.5, 0.75, 0.25, 
    0.5, 0.75, 0.25), receiver.y = c(0.25, 0.45, 0.45, 0.25, 0.45, 
    0.45)), .Names = c("out.x", "out.y", "receiver.x", "receiver.y"
    ), row.names = c(NA, -6L), class = "data.frame")


the_points <- data.frame(point=factor(LETTERS[1:3]), 
    x = c(.25, .5, .75), 
    y = c(.45, .25, .45)
)

## Plot the base graph minus the edges
root <- ggplot() + 
    geom_point(data=the_points, aes(x=x, y=y), size=12, inherit.aes = FALSE) +
    geom_text(data=the_points, aes(x=x, y=y, label=as.character(point)), 
        inherit.aes = FALSE, color="white") +
    ylim(c(.20, .75)) + xlim(c(.25, .75)) +
    ylab("") + xlab("") 

## Add the edges
root + geom_segment(aes(x= out.x, y= out.y, xend = receiver.x, 
        yend = receiver.y), alpha = .7, size = 3, data = so) 

2 个答案:

答案 0 :(得分:10)

这是一种处理来自Hmisc的bezier曲线的方法(由http://is-r.tumblr.com/post/38459242505/beautiful-network-diagrams-with-ggplot2推动)

library(Hmisc)
library(plyr)
# a function to sample a point within a triangle
rtriang <- function(A ,B,C){
  r <- runif(2)
  sqr1 <- sqrt(r[1])
  (1- sqr1)*A +  (1-r[2])*sqr1*B + r[2]*sqr1*C

}


# a function to make a curve between two points (as set up in the example)
make.curve <- function(coords,n=101,A ,B ,C){
  rt  <- rtriang(A,B,C)
  xxs <- unlist(coords[,c(1,3)])
  yys <-unlist(coords[,c(2,4)])
  xx <- append(xxs, rt[1],1)
  yy <- append(yys, rt[2] ,1)
  as.data.frame(bezier(xx,yy, evaluation=n))
}

# A triangle 1 /3 rd size with same centre point
mid <- matrix(colMeans(the_points[,2:3]), ncol=2,nrow=3,byrow=TRUE)
tri <- as.matrix(the_points[,2:3])
rownames(tri) <- rownames(mid) <- LETTERS[1:3]
newT <- mid + (tri-mid)/3

# create a new data set with bezier curves with a midpoint
# somewhere within a triangle 1/3 the size of the original 
newd <- adply(so, 1, make.curve, A = newT['A',],B = newT['B',], C = newT['C',])
newd$id <- rep(seq_len(nrow(so)), each = 101)
# and the plot
root + geom_path(data = newd, aes(colour = factor(id), x=x,y=y))

enter image description here

答案 1 :(得分:8)

我认为使用igraph库在这里可能非常有用,例如:

library(igraph)
# make a data frame for the relationships between
# each of the points, replacing your 'edges' data
pts <- data.frame(a=rep(1:3,2),b=rep(c(2,3,1),2))
# plot using igraph
plot(
  graph.data.frame(pts),
  layout=as.matrix(the_points[c("x","y")]),
  margin=c(-0.3),
  vertex.label=c("A","B","C"),
  vertex.label.color="white",
  vertex.label.family="sans",
  vertex.label.font=2,
  vertex.size=35,
  vertex.color="black",
  edge.arrow.mode="-",
  edge.curved=rep(c(0.5,0.2),3),
  edge.width=5,
  edge.color=1:6
)

导致:

enter image description here