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

技术问询:不改变图例键高度时调整图例垂直间距(附data.frame数据)

Adjust Legend Vertical Spacing Without Changing Key Height (ggplot2)

Got it! To tweak the vertical spacing between legend items while keeping your legend keys at a fixed height, ggplot2's theme system has exactly what you need. Here's a step-by-step solution using your data:

Step 1: Prepare Your Data

First, let's formalize your data frame (I added a grouping variable to create a legend—adjust this to match your actual plot's legend source):

# Your provided data
df <- data.frame(
  tval = c(0.15992376, 0.10649188, 0.50782252, 0.40839714, 0.87263438, -0.67082150, 
           1.04550767, 1.55295583, 1.91169754, -0.25082386, -0.56745556, 0.19166267, 
           2.29615058, 1.40662861, 1.91100408, -0.06249463, -2.38623303, -1.84545755),
  V2 = c(-0.8343450463, -0.1661683709, -0.3090289856, 1.3591006969, -0.1140296582, 
         0.5563068416, -0.0835169157, 0.9997731998, 0.3246076173, 0.0638465365, 
         0.9553410876, -0.4120324771, -1.9694192327, -1.0248516180, -0.4167153416, 
         -0.1230196521, -0.2223423823, -0.18867714)
)

# Add a grouping variable for the legend (customize this to your use case)
df$group <- rep(c("Group 1", "Group 2", "Group 3"), each = 6)

Step 2: Create the Base Plot

Let's build a basic scatter plot with a legend (adjust the geom/aesthetics to match your actual plot):

library(ggplot2)

base_plot <- ggplot(df, aes(x = tval, y = V2, color = group)) +
  geom_point(size = 3) +
  labs(title = "Plot with Custom Legend Spacing", x = "tval", y = "V2")

Step 3: Adjust Legend Spacing (Keep Key Height Fixed)

Use theme() to control two critical parameters:

  • legend.key.height: Fixes the height of each legend key (set this to your desired size).
  • legend.spacing.y: Adjusts the vertical gap between legend items (increase/decrease this value to change spacing).
custom_legend_plot <- base_plot +
  theme(
    # Set fixed legend key height (adjust the unit value as needed)
    legend.key.height = unit(1, "cm"),
    # Adjust vertical spacing between legend entries (tweak this number)
    legend.spacing.y = unit(0.6, "cm"),
    # Optional: Remove extra padding around the legend box for a tighter layout
    legend.margin = margin(t = 0, r = 0, b = 0, l = 0, unit = "cm")
  )

# View the final plot
custom_legend_plot

Key Explanations:

  • legend.key.height: Ensures your legend keys stay the same height no matter what spacing you set. Use units like cm, mm, or lines to match your needs.
  • legend.spacing.y: Controls the empty space between each legend item. Make this value larger for more spacing, smaller for tighter spacing.
  • legend.margin: Optional—removes any default padding around the legend box if you want a more compact legend.

If you're using base R graphics instead of ggplot2, let me know and I can share that approach too!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:41:15