本文介绍了向三角形中心弯曲(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 r sqr1 } #在两点之间作曲线(如示例中所设置的) make.curve< - 函数(coords,n = 101,A,B,C){ rt rtriang(A,B,C) xxs< ]) yys< -unlist(coords [,c(2,4)]) xx yy 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 #创建一个带有中点的贝塞尔曲线的新数据集#在三角形内某处1/3原始 newd 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 1Graph 2 (color only used to highlight desired output)library(ggplot2); library(scales)## The vertices/points data point x y1 A 0.25 0.452 B 0.50 0.253 C 0.75 0.45## The edges data out.x out.y receiver.x receiver.y1 0.25 0.45 0.50 0.252 0.50 0.25 0.75 0.453 0.75 0.45 0.25 0.454 0.25 0.45 0.50 0.255 0.50 0.25 0.75 0.456 0.75 0.45 0.25 0.45## Edges and vertices/points data in dput form for easeso <- 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 edgesroot <- 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 edgesroot + 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 trianglertriang <- 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 pointmid <- 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 plotroot + geom_path(data = newd, aes(colour = factor(id), x=x,y=y)) 这篇关于向三角形中心弯曲(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 09:15