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

使用ggplot2的coord_polar()时如何在面板外添加文本?

解决coord_polar()下环形图例外侧添加文本的问题

问题描述

需要创建连续变量的环形图例图表,要在环形外侧(原x轴下方区域)添加方向文本,但使用annotate()或geom_text()配合coord_polar()时无法达到预期效果。

数据与原绘图代码

数据定义

# DATA
aspect_class = data.frame(from = c(0, seq(22.5, 337.5, by = 45)),
                          to =   c(seq(22.5, 337.5, by = 45), 360),
                          mid = seq(0, 360, by = 45),
                          class = c("North (0°-22.5°)", 
                                    "Northeast (22.5°-67.5°)",
                                    "East (67.5° - 112.5°)",
                                    "Southeast (112.5°-157.5°)",
                                    "South (157.5°-202.5°)",
                                    "Southwest (202.5°-247.5°)",
                                    "West (247.5°-292.5°)",
                                    "Northwest (292.5°-337.5°)",
                                    "North (337.5°-360°)"))

degrees = seq(0, 360, by = 1)
values = sin(degrees * pi / 180)
aspect_legend_df = data.frame(degrees, values)
compas = data.frame(degrees = seq(0,315,45),
                    direction = c("N", "NE", "E", "SE", "S", "SW", "W", "NW"))

原绘图代码

# PLOT
ggplot() +
  geom_tile(data = aspect_legend_df, 
            aes(x = degrees, y = 0, fill = values), 
            width = 1, height = 0.1) +
  scale_x_continuous(breaks = compas$degrees, 
                     labels = ~ paste0(.x, "\u00B0"),
                     name = "Aspect [°]") +
  scale_fill_viridis_c() +
  geom_vline(data = aspect_class[aspect_class$mid %in% c(0, 90, 180, 270),],
             aes(xintercept = mid), 
             color = "black", linewidth = 0.8) +
  geom_vline(data = aspect_class[!aspect_class$mid %in% c(0, 90, 180, 270, 360),],
             aes(xintercept = mid), 
             color = "black", linewidth = 0.5, linetype = "dotdash") +
  annotate(geom = "text", x = compas$degrees, y = 0.06, label = "") +    
  coord_polar(start = 0) + 
  theme_minimal() +
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text.y = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.x = element_text(size = 12, color = "black")) 

解决方案

问题核心是coord_polar()转换后,y轴代表环形的半径,原代码设置的y=0.06处于环形内部,且与原有刻度文本重叠。要将文本放在外侧,需调整y值到环形外部区域,并处理轴文本重叠问题。

修改后的绘图代码

ggplot() +
  # 绘制环形颜色带
  geom_tile(data = aspect_legend_df, 
            aes(x = degrees, y = 0, fill = values), 
            width = 1, height = 0.1) +
  # 隐藏原有x轴刻度文本,避免重叠
  scale_x_continuous(breaks = compas$degrees, 
                     labels = NULL,
                     name = "") +
  scale_fill_viridis_c() +
  # 添加分隔线
  geom_vline(data = aspect_class[aspect_class$mid %in% c(0, 90, 180, 270),],
             aes(xintercept = mid), 
             color = "black", linewidth = 0.8) +
  geom_vline(data = aspect_class[!aspect_class$mid %in% c(0, 90, 180, 270, 360),],
             aes(xintercept = mid), 
             color = "black", linewidth = 0.5, linetype = "dotdash") +
  # 添加外侧方向文本(y值设为0.2,确保在环形外部)
  geom_text(data = compas,
            aes(x = degrees, y = 0.2, label = direction), 
            inherit.aes = FALSE, 
            size = 5, fontface = "bold") +
  # 可选:添加外侧度数文本,与方向文本分层
  geom_text(data = compas,
            aes(x = degrees, y = 0.15, label = paste0(degrees, "\u00B0")), 
            inherit.aes = FALSE, 
            size = 3.5) +
  coord_polar(start = 0) + 
  theme_minimal() +
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text.y = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.x = element_blank())

关键说明

  • coord_polar()下,y值越大文本离中心越远,设置y=0.2可将方向文本固定在环形外侧
  • 若需同时显示度数和方向,可设置不同y值实现分层展示
  • 隐藏原有axis.text.x避免与自定义文本重叠

内容的提问来源于stack exchange,提问作者hopsasa.tralala

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 02:23:12