首页 > 解决方案 > 用 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))

产生这个:

在此处输入图像描述

标签: rggplot2

解决方案


解决方案是:

## 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))

在此处输入图像描述


推荐阅读