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

时间:2018-11-09 08:27:41

标签: r ggplot2

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

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

2 个答案:

答案 0 :(得分:4)

与@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

答案 1 :(得分:3)

此解决方案基于 @Mojoesque 的注释,该注释使用逐段技能将x轴划分为多个子间隔,然后执行多个stat_function()purrr::reduce()。约束在于需要渐近。

tan(x)为例:

f <- function(x) tan(x)
asymp <- c(-3/2*pi, -1/2*pi, 1/2*pi, 3/2*pi)
left <- -2 * pi # left border
right <- 2 * pi # right border
d <- 0.001
interval <- data.frame(x1 = c(left, asymp + d),
                       x2 = c(asymp - d, right))

interval # divide the entire x-axis into 5 sections

#          x1        x2
# 1 -6.283185 -4.713389
# 2 -4.711389 -1.571796
# 3 -1.569796  1.569796
# 4  1.571796  4.711389
# 5  4.713389  6.283185

library(tidyverse)

pmap(interval, function(x1, x2) {
       stat_function(fun = f, xlim = c(x1, x2), n = 1000)
     }) %>% reduce(.f = `+`,
                   .init = ggplot(data.frame(x = c(left, right)), aes(x)) +
                             coord_cartesian(ylim = c(-50, 50)))

enter image description here