gpt4 book ai didi

r - 如何将同心椭圆的边缘与R图中的某些垂直线进行匹配

转载 作者:行者123 更新时间:2023-12-04 04:15:34 29 4
gpt4 key购买 nike

背景:我提出以下问题的最终目标是将以下内容转换为函数。简而言之,我在多个同心椭圆(椭圆)上方放置了一条曲线。核心问题是如何匹配这些椭圆上方的曲线,因此,如果曲线发生变化,椭圆也会相应地变化。

详细信息:

更具体地说,我想使用 11个同心椭圆(请参见下图)。在这11个椭圆的外边缘,我需要 13条线段才能附加到曲线上(请参见下图)。下面显示的曲线是柯西分布(请参见图像下方的R代码的最后一行)。但是该曲线可以是任何其他对称曲线(例如,正态分布,t分布)。

问题:

如何确定椭圆的坐标与必须精确连接到椭圆外边缘的线段?

注意:大多数情况下,问题与椭圆背后的数学有关。

(,我尝试了一些操作,并在下面进一步提供了带注释的R代码)。

enter image description here

这是我的R代码:

if(!require(library(plotrix))){install.packages('plotrix') }
library(plotrix) ## A package for drawing ellipses ##

# In the "plotrix":

# "x" and "y" are the coordinates of the center.
# "a" and "b" give the radii of the ovals.


plot(1, ty='n', ann = F, xlim = c(-4, 6), ylim = c(-3, 1.5) ) ## A platform for ellipses


draw.ellipse(x = rep(1, 11), y = rep(-1.2, 11),
a = seq(1, 6, by = .4), b = seq(1/4.5, 6/4.5 , by = .4/4.5 ),
lty = 2, border = 'gray60' ) ## Draw multiple Concentric ellipses ##


AA <- seq(-4, 6, len = 13) ## A range of values on the x-xis just like "xlim" ##
BB <- dcauchy( AA, 1, .95)*5 ## The Height for the AA according to a distribution ##

segments(AA, rep(-1.2, length(AA) ), AA, BB, lty = 3, lwd = 2, col= 'green4' )

curve(dcauchy(x, 1, .95)*5, -4, 6, add = T, col ='magenta', lwd = 3)

最佳答案

library(plotrix)

#AVAILABE DATA
el_x = 1 #Centre of ellipse
el_y = -1.2 #Centre of ellipse
n = 13 #The number of ellipses (13 in this example)
el_a = seq(1, 6, length.out = n) #'a' values
#You likely have 'b' too, but I am computing for now
el_b = seq(1/4.5, 6/4.5, length.out = n)

#Retain only every other 'a' value
if (n %%2 !=0){ #Odd 'n'
el_a_2 = el_a[seq_along(el_a) %% 2 != 0]
} else { #Even 'n'
el_a_2 = el_a[seq_along(el_a) %% 2 == 0] #Modify if necessary
}

#Store curve in a variable for now
cc = curve(dcauchy(x, 1, .95)*5, min(el_x - el_a), max(el_x + el_a))

#Calculate x-values (subtract and add 'a' to 'el_x') of ellipse extremes
AA <- c(el_x - el_a_2, el_x, el_x + el_a_2) #The x-values you need for segments
AA = sort(AA) #To plot lines and points as you want

#Calculate y-values from AA
BB <- dcauchy(AA, 1, .95)*5 #The y-values you need for segments

#Raise the curve to have 0.2 distance (OPTIONAL)
Raise_curve = max(el_b + el_y) + 0.2 - (min(BB) - max(el_b + el_y))

BB = BB + Raise_curve #Raise BB

plot(1, type = 'n', ann = F,
xlim = c(min(el_x - el_a), max(el_x + el_a)),
ylim = c(min(el_y - el_b), max(BB)))

draw.ellipse(x = rep(el_x, n), y = rep(el_y, n),
a = el_a, b = el_b,
lty = 2, border = 'gray60')

segments(x0 = AA, x1 = AA, y0 = el_y, y1 = BB, lty = 3, lwd = 2,
col = c(rep('green3', floor(length(AA)-3)/2),
rep('navy', 3 ),
rep('green3', length(AA) - 3 - floor(length(AA)-3)/2)))

lines(cc$x, cc$y+ Raise_curve, lwd = 2, col = "red")

#ADDITIONAL EXAMPLE
points(AA, rep(-1.2, length(AA) ), pch = c(rep(22, 6), 21, rep(22, 6)),
col = c(rep('blue', 6), 'red', rep('blue', 6)),
bg = c(rep('blue', 6), 'red', rep('blue', 6)), cex = 3)

xx <- seq(AA[7], AA[9], length.out = 1000)
yy = dcauchy(xx, 1, .95)*5
yy = yy+Raise_curve
polygon(c(AA[7], xx, AA[9]), c(el_y, yy, el_y), border = NA,
col= adjustcolor('green', alpha.f = .2) )

enter image description here

关于r - 如何将同心椭圆的边缘与R图中的某些垂直线进行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101356/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com