在R中通过ggplot /网格查找角度和距离 [英] Finding the angle and distance over a ggplot/grid in R

查看:218
本文介绍了在R中通过ggplot /网格查找角度和距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一些A和B数值做了一个ggplot。 (如果可能的话,你可以给我网格的解决方案吗?)
如:

  AB 
2 3
3 7
4 8
5 9
6 2
7 1

现在,从图中可以看出A1和A2,我想测量每个点所覆盖的角度和距离。

我知道如何从一个点计算距离(通过欧式距离公式),而对于角度,它可以计算为矢量的交叉和点积。但是我面临的问题是编码并代表它。
您能帮忙吗?



grid $ C>。这可以在 ggplot2 中完成,我想,但我现在想学习grid,因为ggplot2和lattice是基于它的。这个图有一些问题,例如注释文本的角度真的必须在设备坐标中计算,而不是本地坐标,所以只有当你的网格方块真的是方形时才会看起来正确。我可能会稍后解决,但我现在没有时间。此外,我会想我可以指定默认值,以便每个原语没有 default.units 参数。这应该让你开始。

 图书馆(网格)

grid.newpage()
df< - data.frame(a = c(2,3,4,5,6,7),b = c(3,7,8,9,2,1))

vp < - viewport(x = 0.5,y = 0.5,width = 0.999,height = 0.999,xscale = c(0,1),yscale = c(0,1))
pushViewport(vp)

#视口边框上的矩形(带虚线):
grid.rect(gp = gpar(lty =dashed,col =steelblue))

vp < - viewport(x = 0.5,y = 0.5,width = 0.9,height = 0.9,xscale = c(0,8),yscale = c(0,10),
默认的单元=原生)
pushViewport(vp)

#提取背景网格
grid.polyline(x = rep(0:8,each = 2), y = rep(c(0,10),9),id = rep(1:9,each = 2),
gp = gpar(lty =solid,col =gray),默认。 units =native)
grid.polyline(x = rep(c(0,8),11),y = rep(0:10,each = 2),id = rep(1:11,each = 2),
gp = gpar(lty =solid,col =gray),default.units =native)

#添加标签
grid .text(as.character(0:8),x = 0:8,y = rep(-0.2,9),
gp = gpar(col =gray,fontsize = 12),default.units =native)
grid.text(as.character(0:10),y = 0:10,x = rep -0.2,11),
gp = gpar(col =gray,fontsize = 12),default.units =native)

grid.lines(x = df $ a ,y = df $ b,gp = gpar(col =steelblue),default.units =native)
grid.points(x = df $ a,y = df $ b,gp = gpar对于(i in 1:(nrow(df)-1)){
x0< b = - df $ a [i]
y0 < - df $ b [i]
x1 < - df $ a [i + 1]
y1 < - df $ b [i +1]
dx < - x1-x0
dy < - y1-y0
dist < - sqrt(dx ^ 2 + dy ^ 2)
ang< - (180 / 3.14159)* atan2(dy,dx)
txt < - sprintf(D:%.1f Ang:%1f,dist,ang)
xt < - (x0 + x1)/ 2
yt < - (y0 + y1)/ 2 + 0.2 * abs(dy / dx)
grid.text(txt,x = xt,y = yt,rot = ang ,
gp = gpar(col =steelblue,fontsize = 9),default.units =native)
}

p>

I have made a ggplot using some A and B numeric values. (If possible can you give me the solution for grid too?) Such as:

A   B
2   3
3   7
4   8
5   9
6   2
7   1

Now from the points, lets say A1 and A2 as shown in diagram, I want to measure the angle and the distance covered from each point.

I know how to calculate the distance (via euclidean distance formula) from one point and for angle it can be calculated as cross and dot product of the vectors. But I am facing the problem to code this and to represent it. Can you help?

解决方案

Okay, here is a first pass - doing it in grid. This could be done in ggplot2 too I imagine, but I want to learn grid for now since ggplot2 and lattice are based on it. This plot has some issues, for example the angle of the annotation text really has to be calculated in device coordinates, not native coordinates, so it only looks right if your grid squares are really square. I might fix that later, but I don't have time now. Also I would think I could specify the defaults so that each primitive doesn't have that default.units parameter. This should get you started though.

library(grid)

grid.newpage()
df <- data.frame(a=c(2,3,4,5,6,7),b=c(3,7,8,9,2,1))

vp <- viewport(x=0.5,y=0.5,width=0.999,height=0.999,xscale=c(0,1),yscale=c(0,1))
pushViewport(vp)

# a rectangle (with dashed lines) on the border of the viewport:
grid.rect(gp=gpar(lty="dashed",col="steelblue"))

vp <- viewport(x=0.5,y=0.5,width=0.9,height=0.9,xscale=c(0,8),yscale=c(0,10),
               default.units="native")
pushViewport(vp)

#draw the background grid
grid.polyline(x=rep(0:8,each=2),y=rep(c(0,10),9),id=rep(1:9,each=2),
              gp=gpar(lty="solid",col="gray"),default.units="native")
grid.polyline(x=rep(c(0,8),11),y=rep(0:10,each=2),id=rep(1:11,each=2),
              gp=gpar(lty="solid",col="gray"),default.units="native")

# add the lables
grid.text(as.character(0:8),x=0:8,y=rep(-0.2,9),
          gp=gpar(col="gray",fontsize=12),default.units="native")
grid.text(as.character(0:10),y=0:10,x=rep(-0.2,11),
          gp=gpar(col="gray",fontsize=12),default.units="native")

grid.lines(x=df$a,y=df$b,gp=gpar(col="steelblue"),default.units="native")
grid.points(x=df$a,y=df$b,gp=gpar(col="steelblue"),default.units="native")


for (i in 1:(nrow(df)-1)){
   x0 <- df$a[i]
   y0 <- df$b[i]
   x1 <- df$a[i+1]
   y1 <- df$b[i+1]
   dx <- x1-x0
   dy <- y1-y0
   dist <- sqrt( dx^2 + dy^2 )
   ang <- (180/3.14159)*atan2(dy,dx)
   txt <- sprintf("D: %.1f  Ang:%.1f",dist,ang)
   xt <- (x0+x1)/2
   yt <- (y0+y1)/2 + 0.2*abs(dy/dx)
   grid.text(txt,x=xt,y=yt,rot=ang,
             gp=gpar(col="steelblue",fontsize=9),default.units="native")
}

这篇关于在R中通过ggplot /网格查找角度和距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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