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

如何调整R语言ggplot中线形图的网格行高度?

Fixing Excessive Vertical Spacing Between Grid Rows in ggplot2 Line Charts

Hey there! Let’s get those grid lines looking tighter. The vertical spacing between horizontal grid rows in your ggplot is mostly controlled by the y-axis scale breaks and theme settings. Here are a few straightforward fixes:

1. Adjust Y-Axis Breaks to Add More Grid Lines

The most direct way to reduce vertical spacing is to increase the number of y-axis breaks (which correspond to grid lines). Use scale_y_continuous() to define custom break points that are closer together.

For example, if your DTF values range from 0 to 20, you could set breaks every 2 units instead of the default sparser intervals:

ggplot() + 
  geom_line(data = chart_data, aes(x = NRO, y = DTF, color = leyenda), size = 1) +
  scale_y_continuous(breaks = seq(0, 20, 2)) # Adjust min, max, and step to match your data

If you don’t know the exact range of your DTF values, let ggplot calculate breaks dynamically:

ggplot() + 
  geom_line(data = chart_data, aes(x = NRO, y = DTF, color = leyenda), size = 1) +
  scale_y_continuous(breaks = function(x) seq(floor(x[1]), ceiling(x[2]), by = 1)) # Step of 1 for tight spacing

2. Fine-Tune Theme Grid Settings

If you already have the right number of breaks but want to refine the visual appearance, adjust the theme’s grid elements. You can tweak line thickness, color, or even add minor grid lines for extra density:

ggplot() + 
  geom_line(data = chart_data, aes(x = NRO, y = DTF, color = leyenda), size = 1) +
  scale_y_continuous(breaks = seq(0, 20, 2)) +
  theme(
    panel.grid.major.y = element_line(size = 0.5, color = "#e0e0e0"), # Soften major grid lines
    panel.grid.minor.y = element_line(size = 0.25, color = "#f0f0f0") # Add minor lines for tighter spacing
  )

3. Compress Spacing with Scaling (If Appropriate)

If your DTF data has a very large range, consider a logarithmic scale (only if your data has no zero/negative values) to compress the vertical spacing:

ggplot() + 
  geom_line(data = chart_data, aes(x = NRO, y = DTF, color = leyenda), size = 1) +
  scale_y_log10(breaks = scales::log_breaks(n = 10)) # Adjust number of breaks for desired density

Modified Version of Your Original Code

Here’s how to integrate these fixes into your script:

library(ggplot2)
library(reshape2)

data <- read.csv('/Users/keepo/Desktop/G.Con/Int18/input-int18.csv')
chart_data <- melt(data, id='NRO')
names(chart_data) <- c('NRO', 'leyenda', 'DTF')

ggplot() + 
  geom_line(data = chart_data, aes(x = NRO, y = DTF, color = leyenda), size = 1) +
  scale_y_continuous(breaks = seq(min(chart_data$DTF), max(chart_data$DTF), by = 1)) # Tweak 'by' for spacing
  theme(
    panel.grid.major.y = element_line(color = "lightgray"),
    panel.grid.minor.y = element_blank() # Remove minor lines if you don't need them
  )

Just adjust the by parameter in seq() to get the exact spacing you want—smaller values mean tighter grid lines.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:32:57