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

R语言echarts4r:将X轴移至Y=-100并显示全部日期标签

问题解决方法

1. 将X轴移至Y=-100位置

要把X轴从默认的Y=0位置移到Y=-100,需要关闭X轴与Y=0的绑定关系,同时固定Y轴范围确保-100处于图表底部:

# 核心配置
e_x_axis(axisLine = list(onZero = FALSE), position = "bottom")
e_y_axis(min = -100, max = 100)

2. 显示所有日期刻度(按天)

通过设置X轴为分类类型,强制显示所有刻度,并指定日期格式化规则:

# 核心配置
e_x_axis(
  type = "category",
  axisLabel = list(
    interval = 0,  # 显示全部刻度
    formatter = "{yyyy-MM-dd}",  # 日期格式化为年-月-日
    rotate = 45  # 旋转标签避免重叠(可选)
  )
)

整合后的完整代码

将上述配置整合到原代码中,最终可运行的代码如下:

####################
# interactive plot #
####################

library(tidyverse)
library(lubridate)
library(echarts4r)

birthdate <-  ymd("1980-04-20")

timeline <- seq(ymd(Sys.Date()-14), (ymd(Sys.Date()+14)), by = "days")

t <- as.numeric(timeline - birthdate)

physical <-  sin(2*pi*t/23)*100

emotional <-  sin(2*pi*t/28)*100

intellectual <-  sin(2*pi*t/33)*100

df <- tibble(timeline,t, physical, emotional, intellectual)  

df <- df |> pivot_longer(cols = c('physical', 'emotional', 'intellectual'),
                         names_to = 'biorhythms',
                         values_to = 'value')

df$biorhythms <- as.factor(df$biorhythms)

# 修改后的交互式图表
df |> dplyr::group_by(biorhythms) %>% 
  e_charts(x = timeline) |> 
  e_line(value) |>
  e_format_y_axis(suffix = "%") |>
  # 配置X轴:移至底部+显示所有日期
  e_x_axis(
    type = "category",
    axisLine = list(onZero = FALSE),
    position = "bottom",
    axisLabel = list(
      interval = 0,
      formatter = "{yyyy-MM-dd}",
      rotate = 45
    )
  ) |>
  # 固定Y轴范围,确保X轴对齐到Y=-100
  e_y_axis(min = -100, max = 100) |>
  e_mark_line(data = list(xAxis = Sys.Date()), title = "Today", symbol = 'none') |>
  e_title("Biorhythm Pseudo-Science") |>
  e_tooltip() |> 
  e_theme("dark-digerati")

关键修改说明

  • axisLine = list(onZero = FALSE):取消X轴与Y=0的绑定,让其可以显示在图表底部。
  • e_y_axis(min = -100, max = 100):强制Y轴范围覆盖-100到100,确保X轴精准对齐到Y=-100位置。
  • axisLabel = list(interval = 0):强制显示所有日期刻度,避免自动隐藏部分日期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 22:45:39