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))
产生这个:
解决方案
解决方案是:
## 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))
推荐阅读
- mysql - 具有多个 OR 的查询结果不是 OR 的顺序
- mysql - 如果我无法触及插入查询,有没有办法在 sql 中回滚失败的批量插入?也许触发?
- javascript - 为什么我的对象没有被推入不同的数组?
- angular - Angular - 这是编写初始化函数的另一种方式吗?
- java - Java JDBC mysql中的更新查询不起作用
- python - 为什么我可以打印到命令行但不能使用 docker 写入文本文件?
- java - 为什么我自己的 AtomicLong 比 JDK 中提供的要慢?
- javascript - 为什么在条件渲染中解析 Math.mod() 的错误?
- python - 有没有办法在包含字符串的对象数组上使用 numpy 字符串数组函数?
- react-native - 为什么在使用 Firebase 注册用户时,Android 和 IOS 设备模拟器之间存在差异?