R中n边多边形的坐标
Coordinates of an n-sided polygon in R
我的目标是开发一个 R 脚本,该脚本 returns 构建单位圆内接的 n 边多边形所需的 (s, c) 坐标集。
我正在关注 this article by Phillip Burger on how to create a radar-chart in Tableau. His approach uses R in order to calculate a background image. His code to return the set of (s, c) coordinates necessary to build a 5-sided polygon can be found 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 的 n 边多边形。
polyCoords<-function(n){
sq<-2*pi*(0:n)/n
cbind(sin(sq),cos(sq))
}
plot(polyCoords(10),type='l')
请注意,它重复了第一个坐标,因此该图将被关闭。如果您不想这样,请将 0:n
更改为 1:n
。
我的目标是开发一个 R 脚本,该脚本 returns 构建单位圆内接的 n 边多边形所需的 (s, c) 坐标集。
我正在关注 this article by Phillip Burger on how to create a radar-chart in Tableau. His approach uses R in order to calculate a background image. His code to return the set of (s, c) coordinates necessary to build a 5-sided polygon can be found 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 的 n 边多边形。
polyCoords<-function(n){
sq<-2*pi*(0:n)/n
cbind(sin(sq),cos(sq))
}
plot(polyCoords(10),type='l')
请注意,它重复了第一个坐标,因此该图将被关闭。如果您不想这样,请将 0:n
更改为 1:n
。