R中的n边多边形的坐标

时间:2015-03-20 16:57:51

标签: r coordinates polygon radar-chart

我的目标是开发一个R脚本,它返回构建单位圆内的n边多边形所需的(s,c)坐标集。

我正在关注Phillip Burger关于如何在Tableau中创建雷达图的this article。他的方法使用R来计算背景图像。他的代码可以找到构建5边多边形所需的(s,c)坐标集here

基于Phillip Burger的代码,是否有人建议如何修改他的代码以生成n边多边形坐标?

我正在处理一个商业案例,我需要n = 3和n = 10组坐标。

非常感谢,

皮耶罗

编辑:我所需的输出格式是一个数据框,其中包含列pathID; pathOrder; xCoordinate; yCoordinate - 稍后在Tableau中绘制线条是必要的。

以下是5边多边形的示例输出

pathID;pathOrder;xCoordinate;yCoordinate
61;1;0;0
62;1;0;0
63;1;0;0
64;1;0;0
65;1;0;0
61;2;0;1,1
62;2;1,046162168;0,339918694
63;2;0,646563778;-0,889918694
64;2;-0,646563778;-0,889918694
65;2;-1,046162168;0,339918694
0;1;0;0
0.25;1;0;0,25
0.5;1;0;0,5
0.75;1;0;0,75
1;1;0;1
0;2;0;0
0.25;2;0,237764129;0,077254249
0.5;2;0,475528258;0,154508497
0.75;2;0,713292387;0,231762746
1;2;0,951056516;0,309016994
0;3;0;0
0.25;3;0,146946313;-0,202254249
0.5;3;0,293892626;-0,404508497
0.75;3;0,440838939;-0,606762746
1;3;0,587785252;-0,809016994
0;4;0;0
0.25;4;-0,146946313;-0,202254249
0.5;4;-0,293892626;-0,404508497
0.75;4;-0,440838939;-0,606762746
1;4;-0,587785252;-0,809016994
0;5;0;0
0.25;5;-0,237764129;0,077254249
0.5;5;-0,475528258;0,154508497
0.75;5;-0,713292387;0,231762746
1;5;-0,951056516;0,309016994
0;6;0;0
0.25;6;0;0,25
0.5;6;0;0,5
0.75;6;0;0,75
1;6;0;1

这是我要修改的R代码片段

# Name: radar-chart-pentagon.R
# Author: Phillip Burger
# Date: August 11, 2013
# Purpose: return the set of (s, c) coordinates necessary to build a pentagon 
# inscribed in a unit circle. Includes three, inner polygons at 75 percent,
# 50 percent, and 25 percent of unit radius. Shape is oriented with center point
# at (0, 0). Original inspiration was to provide coordinates necessary to build
# a pentagon-shaped radar chart in Tableau, where generated points are used to 
# define a path within Tableau. Includes path coordinates for three sample 
# departments. 
# Posted: http://www.phillipburger.net/wordpress/2013/08/11/radar-chart-in-tableau/
# Reference: For pentagon coordinates, http://mathworld.wolfram.com/Pentagon.html
# ToDo: Curently working only working for the shape of a pentagon. Next steps:
# 1) extend to general case of n-sided polygon
# 2) extend to t-intermediate (inner) polygons.

suppressPackageStartupMessages(require(aspace))

SHAPE <- "pentagon"
LINE <- "line"
RADIUS <- 1 # unit radius circle
CENTERX <- 0  # in radians
CENTERY <- 0  # in radians

setwd("C:/Users/Dropbox")
outputFile <- paste("radar-chart-pentagon-with-deptsample", ".txt", sep = "")	

shapePoints <- matrix(NA, 1, 4, dimnames = NULL)
linePoints <- matrix(NA, 1, 4, dimnames = NULL)


polyCoords<-function(n){
	sq<-2*pi*(0:n)/n
	cbind(sin(sq),cos(sq))
}
plot(polyCoords(10),type='l')


# Function: coordinates
# Purpose: calculate the coordinates of a polygon
# Parameters: radius - radius of the polygon
# Returns: matrix containing coordinates of five polygons including origin
coordinates <- function(radius) {	
	cCoord1 <- radius * cos(2 * pi / 5)
	cCoord2 <- radius * cos(pi / 5)
	sCoord1 <- radius * sin(2 * pi / 5)
	sCoord2 <- radius * sin(4 * pi / 5)
	cat("c and s values for radius: ", radius, " \n", sep = "")
	cat("c1: ", cCoord1, " \n", sep = "") 
	cat("c2: ", cCoord2, " \n", sep = "") 
	cat("s1: ", sCoord1, " \n", sep = "") 
	cat("s2: ", sCoord2, " \n\n", sep = "")
	pointsBuild <- matrix(NA, 1, 4, dimnames = NULL)
	pointsBuild <- rbind(pointsBuild, c(radius, 1, 0, radius))
	pointsBuild <- rbind(pointsBuild, c(radius, 2, sCoord1, cCoord1))
	pointsBuild <- rbind(pointsBuild, c(radius, 3, sCoord2, -cCoord2)) 
	pointsBuild <- rbind(pointsBuild, c(radius, 4, -sCoord2, -cCoord2))
	pointsBuild <- rbind(pointsBuild, c(radius, 5, -sCoord1, cCoord1)) 
	pointsBuild <- rbind(pointsBuild, c(radius, 6, 0, radius))
	return(pointsBuild)
}

#
# Create five polygons
#
for(radius in c(1.0, 0.75, 0.50, 0.25, 0)) {
	shapePoints <- rbind(shapePoints, coordinates(radius = radius))
}

# Initialize output data
linePoints <- rbind(linePoints, coordinates(radius = RADIUS))

shapePoints <- cbind(rep(SHAPE, nrow(shapePoints)), shapePoints)
linePoints <- cbind(rep(LINE, nrow(linePoints)), linePoints)
dfShapePoints <- as.data.frame(na.omit(rbind(shapePoints, linePoints)))

names <- c("shape",
		"pathID",
		"pathOrder",
		"xCoordinate",
		"yCoordinate")
names(dfShapePoints) <- names

# Write output file
write.table(dfShapePoints[ , ], 
		file = outputFile,
		sep = "\t",
		append = FALSE,
		col.names = TRUE,
		row.names = FALSE,
		quote = FALSE)

1 个答案:

答案 0 :(得分:4)

此代码将绘制半径为1的n边多边形。

polyCoords<-function(n){
  sq<-2*pi*(0:n)/n
  cbind(sin(sq),cos(sq))
}
plot(polyCoords(10),type='l')

请注意,它会重复第一个坐标,以便关闭绘图。如果您不想这样做,请将0:n更改为1:n

enter image description here