gpt4 book ai didi

r - 如何处理ggplot2中的垂直渐近线

转载 作者:行者123 更新时间:2023-12-04 10:37:11 25 4
gpt4 key购买 nike

考虑三个简单的数学函数:

f1 <- function(x) 1/x
f2 <- function(x) tan(x)
f3 <- function(x) 1 / sin(x)

分别存在一定的垂直渐近线,即当 x 接近某个值时,f(x) 几乎为无穷大。我通过 ggplot2::stat_function() 绘制了这三个函数:
# x is between -5 to 5
ggplot(data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = f1, n = 1000) +
coord_cartesian(ylim = c(-50, 50))

# x is between -2*pi to 2*pi
ggplot(data.frame(x = c(-2*pi, 2*pi)), aes(x)) +
stat_function(fun = f2, n = 1000) +
coord_cartesian(ylim = c(-50, 50))

# x is between -2*pi to 2*pi
ggplot(data.frame(x = c(-2*pi, 2*pi)), aes(x)) +
stat_function(fun = f3, n = 1000) +
coord_cartesian(ylim = c(-50, 50))

enter image description here

渐近线分别出现在:
x1 <- 0
x2 <- c(-3/2*pi, -1/2*pi, 1/2*pi, 3/2*pi)
x3 <- c(-pi, 0, pi)

实际上,这些行并不存在,但是 ggplot使它们可见。我试图使用 geom_vline()覆盖它们,即:
+ geom_vline(xintercept = x1, color = "white")
+ geom_vline(xintercept = x2, color = "white")
+ geom_vline(xintercept = x3, color = "white")

输出看起来很粗糙,可以看到模糊的黑色标记。有没有更健壮的方法?

enter image description here

最佳答案

与@Mojoesque 的评论相关的解决方案并不完美,但也相对简单,并且有两个小缺点:需要知道渐近线(x1x2x3)并可能减小y的范围”。

eps <- 0.01
f1 <- function(x) if(min(abs(x - x1)) < eps) NA else 1/x
f2 <- function(x) if(min(abs(x - x2)) < eps) NA else tan(x)
f3 <- function(x) if(min(abs(x - x3)) < eps) NA else 1 / sin(x)

ggplot(data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = Vectorize(f1), n = 1000) +
coord_cartesian(ylim = c(-30, 30))

ggplot(data.frame(x = c(-2*pi, 2*pi)), aes(x)) +
stat_function(fun = Vectorize(f2), n = 1000) +
coord_cartesian(ylim = c(-30, 30))

ggplot(data.frame(x = c(-2*pi, 2*pi)), aes(x)) +
stat_function(fun = Vectorize(f3), n = 1000) +
coord_cartesian(ylim = c(-30, 30))

enter image description here

关于r - 如何处理ggplot2中的垂直渐近线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53222160/

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