r - 螺旋环绕的文本

标签 r plot

我在 LaTeX people wrapping text into a spiral 看到的如下所示。我想在 R 中复制它。

enter image description here

我虽然情节 arctext会这样做,但如果有足够的文字,它似乎会形成一个圆圈,如下图(左)所示。我可以制作图中所示的螺旋线(右),但无法合并文本和螺旋线。

enter image description here

代码

txt <- paste(rep("bendy like spaghetti", 10), collapse=" ")
txt2 <- paste(rep("bendy like spaghetti", 20), collapse=" ")

par(mfrow=c(1, 2), mar=rep(.3, 4)+c(0, 0, 1, 0))

library(plotrix)
plot.new()
plot.window(xlim = c(1, 5), ylim = c(2, 4), asp = 1)
arctext(txt, center = c(3, 3), radius = 1.7, 
  start = 4 * pi / 3, cex = .75, clockwise = FALSE)
title(main = "Arc Text (plotrix)")

theta <- seq(0, 30 * 2 * pi, by = 2 * pi/72)
x <- cos(theta)
y <- sin(theta)
R <- theta/max(theta)
plot.new()
plot.window(xlim = c(-1, 1), ylim = c(-1, 1), asp = 1)
lines(x * R, y * R)
title(main = "A Spiral")

理想情况下,该解决方案适用于长度为 n 的文本,因此 txttxt2以上都会形成一个环绕螺旋,但大小不同( txt2txt 长度的两倍)。

联系方式 网格/ ggplot2 欢迎使用基本的grapgics。

最佳答案

不完美但是

txt <- paste(rep("bendy like spaghetti", 10), collapse=" ")
txt2 <- paste(rep("bendy like spaghetti", 20), collapse=" ")
tt <- strsplit(txt, '')[[1]]

xx <- 5
par(mfrow = c(1,2), mar = c(0,0,0,0))
plot(-xx:xx, -xx:xx, type = 'n', axes = FALSE, ann = FALSE)

## option 1
r <- rev(seq(0, xx, length.out = length(tt)))
x <- sqrt(r) * cos(2 * pi * r)
y <- sqrt(r) * sin(2 * pi * r)
text(x, y, tt)

## option 2
plot(-xx:xx, -xx:xx, type = 'n', axes = FALSE, ann = FALSE)
srt <- atan2(y, x) * 180 / pi
for (ii in seq_along(tt))
  text(x[ii], y[ii], tt[ii], srt = srt[ii] - 90)

enter image description here

显然,越靠近中心,字母之间的距离就越小,因此可以改进。

另外我不知道你怎么能调用 text对于 srt 的每个新值从 srt 开始使用这种方法不是正式的论点意味着你不能 Vectorize(text.default, vectorize.args = 'srt') ,但这对于示例数据来说并不是很慢。

此外,您可以制作数据框并将其插入 ggplot .
dd <- data.frame(x, y, srt = srt - 90, tt)
library('ggplot2')
ggplot(dd, aes(x, y)) + geom_text(label = dd$tt, size = 5)
ggplot(dd, aes(x, y)) + geom_text(label = dd$tt, size = 5, angle = dd$srt)

关于r - 螺旋环绕的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34830677/

相关文章:

r - 使用 input$ 调用数据集

r - 计算每列分组数据的中位数

randomForest的重要性只包含MeanDecreaseGini

r - 老鼠 : glm. 适合:算法没有收敛

python - 在 matplotlib 中将 2D 直方图绘制为热图

r - "zero-length arrow"有多小?

R在数据框中的两列之间随机交换值

r - 如何在美国 map 中将海洋着色为蓝色?

r - 将置信区间添加到分位数回归的样条曲线

c++ - 绘制真实坐标