1

我在 powerpoint 中制作了这张图片来说明我正在尝试做的事情:

在此处输入图像描述

我正在尝试制作一系列沿 x 轴以一致的间隔“移动”的圆圈(每个圆圈的大小相同);例如,每个连续圆的中心距离前一个圆 2 点。

我已经尝试了几件事,包括包中的DrawCircle功能DescTools,但不能产生这个。例如,这里我试图画 20 个圆,其中每个圆的中心距离前一个圆 2 点,每个圆的半径为 2(这不起作用)

library(DescTools)
plotdat <- data.frame(xcords = seq(1,50, by = 2.5), ycords = rep(4,20))
Canvas()
DrawCircle(x=plotdat$xcords, y=plotdat$ycords, radius = 2)

如何在 R 中做到这一点?

4

3 回答 3

2

这基本上是@Peter 的答案,但有一些修改。radius=您的方法很好,但DrawCircle. 有关参数,请参见手册页?DrawCircle

dev.new(width=12, height=4)
Canvas(xlim = c(0,50), ylim=c(2, 6), asp=1, xpd=TRUE)
DrawCircle(x=plotdat$xcords, y=plotdat$ycords, r.out = 2)

绘图画布

但是你的例子有轴:

plot(NA, xlim = c(0,50), ylim=c(2, 6), xlab="", ylab="", yaxt="n", asp=1, xpd=TRUE)
DrawCircle(x=plotdat$xcords, y=plotdat$ycords, r.out = 2)

在此处输入图像描述

于 2020-10-29T23:45:55.777 回答
1

我的解决方案需要创建一些辅助功能

library(tidyverse)

##First function: create circle with a predefined radius, and a x-shift and y-shift
create_circle <- function(radius,x_shift, y_shift){
    p <- tibble(
        x = radius*cos(seq(0,2*pi, length.out = 1000)) + x_shift ,
        y = radius*sin(seq(0,2*pi, length.out = 1000))+ y_shift
    ) 
    
    return(p)
}

##Use lapply to create circles with multiple x shifts:
##Group is only necessary for plotting
l <- lapply(seq(0,40, by = 2), function(i){
    create_circle(2,i,0) %>%
        mutate(group = i)
})

##Bind rows and plot
bind_rows(l) %>%
    ggplot(aes(x = x, y = y, group =group)) + 
    geom_path()

在此处输入图像描述

于 2020-10-29T23:34:47.703 回答
0

这行得通吗?

library(DescTools)

plotdat <- data.frame(xcords = seq(1, 5, length.out = 20), ycords = rep(4,20))

Canvas(xlim = c(0, 5), xpd=TRUE)

DrawCircle(x=plotdat$xcords, y=plotdat$ycords, r.out = 2)


我假设当您说圆心相距 2 点时,您的意思是相距 0.2 个单位。

您可能必须尝试使用​​这些值才能获得所需的值。

在此处输入图像描述

于 2020-10-29T23:22:49.723 回答