用ggplot2绘制圆的线段

时间:2018-08-28 21:51:25

标签: r ggplot2

我想将一个圆分成11个相等的部分,并用ggplot2进行绘制。

我遇到了一些麻烦,因为我的代码无法产生均匀的圆弧段。

代码

## spilt the circle radians into 11 segments
angle_spilt <- (2*pi) / 11
angle_spilt_seq <- seq(0,(2*pi),angle_spilt)
angle_spilt_seq

## create a dataframe for plotting
distance.radius = 100
segment.line.dat <- data.frame(angle = angle_spilt_seq, stringsAsFactors = F)

# calculate new x,y based on angles - (origins at 0,0)
segment.line.dat$yend = distance.radius * sin(((segment.line.dat$angle * 180) / (pi)))
segment.line.dat$xend = distance.radius * cos(((segment.line.dat$angle * 180) / (pi)))
segment.line.dat$x = 0
segment.line.dat$y = 0

## plot the segments 
ggplot() + xlim(c(-110, 110)) + ylim(c(-110, 110)) + geom_segment(data = segment.line.dat, aes(x = x, y = y, xend = xend , yend = yend))

哪个生成此:

enter image description here

1 个答案:

答案 0 :(得分:3)

解决方案是:

## spilt the circle radians into 11 segments
angle_spilt <- (2*pi) / 11
angle_spilt_seq <- seq(0,(2*pi),angle_spilt)
angle_spilt_seq

## create a dataframe for plotting
distance.radius = 100
segment.line.dat <- data.frame(angle = angle_spilt_seq, stringsAsFactors = F)

# calculate new x,y based on angles - (origins at 0,0)
segment.line.dat$yend = distance.radius * sin(((segment.line.dat$angle * pi) / (pi)))
segment.line.dat$xend = distance.radius * cos(((segment.line.dat$angle * pi) / (pi)))
segment.line.dat$x = 0
segment.line.dat$y = 0

## plot the segments 
ggplot() + xlim(c(-110, 110)) + ylim(c(-110, 110)) + geom_segment(data = segment.line.dat, aes(x = x, y = y, xend = xend , yend = yend))

enter image description here