向三角形中心弯曲(ggplot2) [英] Bend line toward center of triangle (ggplot2)

查看:205
本文介绍了向三角形中心弯曲(ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个顶点表示为与三条边相连的绘图中的点。我想将边缘弯向三角形的中心( c(.5,.35))。我怎样才能把图1变成ggplot2中的图2(我认为这个答案也可以概括为基础?虽然顶点保持稳定,但在弯曲边缘有一些理想的抖动,我认为这意味着某种线性变换有一些稍微随机化的常量。



图1 b
$ b



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

  library(ggplot2); library(scales)

##顶点/分数据
点xy
1 A 0.25 0.45
2 B 0.50 0.25
3 C 0.75 0.45

##边缘数据
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.7 5 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

##输出形式的边缘和顶点/点数据(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)


##绘制底图减去边缘
root< ;数据= the_points,aes(x = x,y = y),size = 12,inherit.aes = FALSE)+
geom_text x = x,y = y,label = as.character(point)),
inherit.aes = FA LSE,color =white)+
ylim(c(.20,.75))+ xlim(c(.25,.75))+
ylab()+ xlab( )

##添加边
root + geom_segment(aes(x = out.x,y = out.y,xend = receiver.x,
yend = receiver .y),alpha = .7,size = 3,data = so)


解决方案

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

 库(Hmisc)
库(plyr)
#在三角形内采样点的函数
rtriang < - 函数(A,B,C){
r < - runif(2)
sqr1 < - sqrt(r [1])$ ​​b $ b(1- sqr1 )* A +(1-r [2])* sqr1 * B + r [2] * sqr1 * C

}


#在两点之间作曲线(如示例中所设置的)
make.curve< - 函数(coords,n = 101,A,B,C){
rt rtriang(A,B,C)
xxs< ])
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))
}

#三角形1 / (colMeans(the_points [,2:3]),ncol = 2,nrow = 3,byrow = TRUE)
tri< - as。矩阵(the_points [,2:3])
rownames(tri) - - rownames(mid) - - 字母[1:3]
newT < - mid +(tri-mid)/ 3

#创建一个带有中点的贝塞尔曲线的新数据集
#在三角形内某处1/3原始
newd < - adply的大小(所以, 1,make.curve,A = newT ['A',],B = newT ['B',],C = newT ['C',])
newd $ id< - rep(seq_len每个= 101)
#和绘图
root + geom_path(data = newd,aes(color = factor(id),x = x,y = y))


I have 3 vertices represented as points in a plot connected with three edges. I'd like to bend the the edges towards the center of the triangle (c(.5, .35)). How can I turn graph 1 into graph 2 in ggplot2 (I assume this answer would be generalizable to base as well? There is some desirable jitter in the curved edges though the vertices remain stable. I assume this would mean some sort of linear transformation that has some sort of slightly randomized constant.

Graph 1

Graph 2 (color only used to highlight desired output)

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) 

解决方案

Here is an approach working on bezier curves from Hmisc (motivated by 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))

这篇关于向三角形中心弯曲(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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