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

求助:ggplot2中自定义重命名Y轴刻度(需保留geom_line类绘图函数)

Solution for Custom Y-Axis Labels in ggplot2

Got it, let's fix this custom Y-axis labeling issue while keeping your line plot intact. The key is to use scale_y_continuous() to map your original numeric ticks to the custom time-formatted labels you need.

Step-by-Step Fix:

  1. First, define the mapping between your original Y-axis values and the custom labels you want to display.
  2. Add scale_y_continuous() to your ggplot code, specifying the exact numeric breaks and their corresponding custom labels.

Full Working Code:

# Define the mapping of original numeric values to custom time labels
y_breaks <- c(80000, 84000, 88000, 92000)
y_labels <- c("1:20:000", "1:24:000", "1:28:000", "1:32:000")

# Update your plot with custom Y-axis settings
seby_time <- ggplot(tempi_seb, aes(lap, milliseconds), color = position) +
  geom_point() +
  geom_line(color = "red") +
  scale_y_continuous(
    breaks = y_breaks,
    labels = y_labels
    # Optional: Uncomment below to restrict Y-axis range to your custom ticks
    # limits = c(min(y_breaks), max(y_breaks))
  )

Explanation:

  • breaks tells ggplot exactly which numeric values to render on the Y-axis.
  • labels replaces each numeric break with your desired time-formatted string.
  • Your geom_line() and geom_point() layers stay fully functional as required—no need to remove or modify them.

If there are other auto-generated ticks you want to retain, simply add their numeric values to the y_breaks vector and matching labels to y_labels.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 06:42:26