You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Markdown渲染的正切函数图与控制台输出不一致问题

问题:R Markdown编织后正切函数图像异常,控制台显示正常

我用R Markdown做笔记本,包含一系列三角函数图像。其中正切函数图像在RStudio控制台显示正常,但执行相同代码编织R Markdown文档时,图像完全异常。

可复现代码

library(tidyverse)

degrees_bounds <- seq(-720, 720, by = 1)
radians <- map(degrees_bounds, \(x){x * pi / 180}) %>% unlist() %>% round(., 4)            # check
pi_radians <- radians / pi %>% round(., 4)
sine_col <- map(pi_radians, \(x){sin(x)}) %>% unlist() %>% round(., 4)                          # check
cosine_col <- map(pi_radians, \(x){cos(x)}) %>% unlist() %>% round(., 4)                        # check
tangent_col <- (sine_col/cosine_col) %>% unlist() %>% round(., 4)                               # check
cotangent_col <- (cosine_col/sine_col) %>% unlist() %>% round(., 4)                             # check
secant_col <- (1/cosine_col) %>% unlist() %>% round(., 4)                                       # check
cosecant_col <- (1/sine_col) %>% unlist() %>% round(., 4)                                       # check

trig_table <- bind_cols(
    degrees_bounds,
    radians,
    pi_radians, 
    sine_col, 
    cosine_col, 
    tangent_col, 
    cotangent_col, 
    secant_col, 
    cosecant_col)  %>% 
    rename("degrees" = "...1", 
           "radians" = "...2", 
           "pi_radians" = "...3",
           "sine" = "...4",
           "cosine" = "...5",
           "tangent" = "...6",
           "cotangent" = "...7",
           "secant" = "...8",
           "cosecant" = "...9")

### Plot three tangent lines    
    tangent_graph_1_tbl <- trig_table %>% 
    filter(tangent != -Inf) %>% 
    filter(tangent != Inf) %>% 
    filter(pi_radians > 0.5) %>% 
    filter(pi_radians < 1.5)
            
    tangent_graph_2_tbl <- trig_table %>% 
    filter(tangent != -Inf) %>% 
    filter(tangent != Inf) %>% 
    filter(pi_radians > -0.5) %>% 
    filter(pi_radians < 0.5) 
    
    tangent_graph_3_tbl <- trig_table %>% 
    filter(tangent != -Inf) %>% 
    filter(tangent != Inf) %>% 
    filter(pi_radians > 1.5) %>% 
    filter(pi_radians < 2.5) 
    
    tangent_plot <- ggplot() + 
    geom_line(data = tangent_graph_1_tbl, aes(x = pi_radians, y = tangent), color = "red") + 
    geom_line(data = tangent_graph_2_tbl, aes(x = pi_radians, y = tangent), color = "red") + 
    geom_line(data = tangent_graph_3_tbl, aes(x = pi_radians, y = tangent), color = "red") + 
    scale_x_continuous(n.breaks = 9) +
    scale_y_continuous(limits = c(-10, 10)) + 
    labs(
        title = "Tangent Function",
        subtitle = "Range -0.5 Radians to 2.5 Radians",
        caption = TeX(r'(Curves extend to \pm infinity)'), 
        x = "Angle in Pi Radians"
    )

tangent_plot

现象对比

  • RStudio控制台:正切函数的三段曲线(-0.5π到2.5π区间)正常显示
  • R Markdown编织后:图像出现错误连线,曲线形态混乱

解决方案

问题根源

  1. 不必要的round操作导致cosine值被舍入为0,生成极端的Inf/-Inf或超大数值,过滤逻辑无法完全排除
  2. 分三个数据表绘制geom_line,加上编织时的渲染逻辑差异,导致错误连线
  3. 直接截断y轴范围,丢失曲线边界的过渡形态

修正后完整代码

library(tidyverse)
library(scales)  # 用于处理y轴超出范围的点

# 简化数据生成,避免舍入误差
degrees_bounds <- seq(-720, 720, by = 1)
radians <- degrees_bounds * pi / 180
pi_radians <- radians / pi
sine_col <- sin(radians)
cosine_col <- cos(radians)
tangent_col <- sine_col / cosine_col

# 构建数据表并严谨过滤
trig_table <- tibble(
  degrees = degrees_bounds,
  radians = radians,
  pi_radians = pi_radians,
  sine = sine_col,
  cosine = cosine_col,
  tangent = tangent_col
) %>%
  filter(abs(cosine) > 1e-6) %>%  # 排除cosine接近0的点,避免极端值
  filter(between(pi_radians, -0.5, 2.5))  # 直接筛选目标区间

# 单个geom_line绘图,用squish保留曲线边界形态
tangent_plot <- ggplot(trig_table, aes(x = pi_radians, y = tangent)) +
  geom_line(color = "red") +
  scale_x_continuous(n.breaks = 9) +
  scale_y_continuous(limits = c(-10, 10), oob = squish) +
  labs(
    title = "正切函数",
    subtitle = "区间:-0.5π 到 2.5π",
    caption = "曲线向±∞延伸",
    x = "角度(π弧度)"
  )

tangent_plot

额外说明

如果需要保留TeX格式的文本,需在R Markdown的YAML头部添加对应设置,例如:

---
title: "三角函数笔记"
output:
  html_document:
    mathjax: default
---

内容的提问来源于stack exchange,提问作者Charles Knell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 09:00:57