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

R语言绘制带方位玫瑰图时正北扇区不显示的问题求助

解决玫瑰图正北扇区无法绘制的问题

问题原因

你的推测完全正确——正北扇区的多边形跨越了0/2π边界,再加上scale_x_continuous(limits = c(0, 2*pi))的范围限制,导致该扇区被截断无法显示。另外coord_polar()默认x轴0对应正东,但你把正北设为0,也会导致方位位置错位。

修正方案

调整角度映射与极坐标起始参数,让每个扇区都落在坐标轴范围内,同时匹配正确的方位指向:

  1. 重新定义方位对应的弧度:让正北对应3π/2(ggplot极坐标默认0为正东,逆时针旋转,正北在3π/2位置),后续方位按逆时针依次递增π/4。
  2. 设置coord_polar(start = 0)保持默认起始方向,确保方位标签与扇区对应。
  3. 移除scale_x_continuous的limits参数,避免截断边界扇区;或者将范围调整为c(-pi/8, 2*pi + pi/8),给边缘扇区留足显示空间。

修正后的完整代码

set.seed(456)

# 定义方位
aspects <- c("North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest")

# 每个方位的测量次数
n_measurements <- 10

# 创建包含所有方位的向量(每个方位重复对应测量次数)
all_aspects <- rep(aspects, each = n_measurements)

# 模拟盖度百分比数据(随方位变化)
mean_cover <- c(40, 35, 30, 45, 50, 42, 38, 33) 
sd_cover <- 10 

all_cover <- rnorm(length(all_aspects), mean = rep(mean_cover, each = n_measurements), sd = sd_cover)

# 创建数据框
lichen_data <- data.frame(aspect = all_aspects, cover = all_cover)

# 设置因子水平顺序
lichen_data$aspect <- factor(lichen_data$aspect, levels = aspects)

# 计算每个方位的均值和标准误
mean_cover_by_aspect <- tapply(lichen_data$cover, lichen_data$aspect, mean)
se_cover_by_aspect <- tapply(lichen_data$cover, lichen_data$aspect, function(x) sd(x) / sqrt(length(x)))

# 修正:重新定义方位对应的弧度(正北对应3π/2,逆时针依次递增π/4)
angles <- seq(3*pi/2, 3*pi/2 + 2*pi - pi/4, by = pi/4)  

# 创建绘图用数据框
rose_data <- data.frame(aspect = aspects,
                        cover = mean_cover_by_aspect[aspects],
                        angle = angles,
                        se = se_cover_by_aspect[aspects])

# 创建带误差棒的玫瑰图
library(ggplot2)

ggplot(rose_data, aes(x = angle, y = cover)) +
  geom_bar(stat = "identity", width = pi/4, color = "black", fill = "lightblue") +
  geom_errorbar(aes(ymin = cover - se, ymax = cover + se), width = 0.1) +
  coord_polar(start = 0) +  # 保持默认起始方向
  scale_x_continuous(breaks = angles, labels = aspects) +  # 移除limits参数
  scale_y_continuous(limits = c(0, max(rose_data$cover + rose_data$se) * 1.1)) +
  labs(title = "Lichen Cover by Aspect (Rose Diagram)",
       y = "Mean Lichen Percent Cover") +
  theme_minimal() +
  theme(axis.text.x = element_text(size = 10),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size = 12))

补充方案(坚持正北为x轴0位置)

如果想要让正北对应x轴0点,可以设置coord_polar(start = pi/2)(将极坐标起始方向顺时针旋转π/2),同时扩展x轴范围避免截断:

# 角度序列保持原定义
angles <- seq(0, 2*pi - pi/4, by = pi/4)  

ggplot(rose_data, aes(x = angle, y = cover)) +
  geom_bar(stat = "identity", width = pi/4, color = "black", fill = "lightblue") +
  geom_errorbar(aes(ymin = cover - se, ymax = cover + se), width = 0.1) +
  coord_polar(start = pi/2) +  # 起始方向顺时针转π/2,0对应正北
  scale_x_continuous(limits = c(-pi/8, 2*pi + pi/8),  # 扩展范围避免截断
                     breaks = angles,
                     labels = aspects) +
  scale_y_continuous(limits = c(0, max(rose_data$cover + rose_data$se) * 1.1)) +
  labs(title = "Lichen Cover by Aspect (Rose Diagram)",
       y = "Mean Lichen Percent Cover") +
  theme_minimal() +
  theme(axis.text.x = element_text(size = 10),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size = 12))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 15:43:14