求助: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:
- First, define the mapping between your original Y-axis values and the custom labels you want to display.
- 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:
breakstells ggplot exactly which numeric values to render on the Y-axis.labelsreplaces each numeric break with your desired time-formatted string.- Your
geom_line()andgeom_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
相关产品推荐
相关产品推荐

