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

facet_grid中scales="free"参数无效,如何让Summer行数据正常显示?

问题原因

极坐标默认不支持分面自由坐标轴,你手动修改is_free属性的临时适配方法会存在y轴下限适配异常的问题:当夏季分组的计数值远小于其他季节时,分面y轴会自动将下限设为大于0的值,柱状图从大于0的位置开始绘制,在极坐标下就会被挤压甚至完全不显示。

解决步骤
  • 第一步:先验证夏季数据有效性
    单独筛选夏季数据运行绘图代码,确认数据本身不存在缺失、dir.binned/spd.binned因子水平异常的问题。
  • 第二步:固定y轴下限为0,保留上限自动适配
    新增scale_y_continuous配置,强制所有分面y轴从0开始,避免柱状图被截断:
    scale_y_continuous(limits = c(0, NA), expand = expansion(mult = c(0, 0.1)))
    
  • 第三步(可选):替换统计逻辑避免隐式计数异常
    提前手动汇总每个分组的计数,用geom_col替代默认统计计数的geom_bar,逻辑更可控:
    # 提前汇总数据
    library(dplyr)
    plot_data <- data %>% 
      count(Season, year, dir.binned, spd.binned, name = "count")
    
    # 绘图时替换aes和geom层
    ggplot(data = plot_data, aes(x = dir.binned, y = count, fill = spd.binned)) +
      geom_col()
    
修改后的完整示例代码
cp <- coord_polar(start = -((dirres/2)/360) * 2 * pi, dir = 1) 
cp$is_free <- function() TRUE

# 提前汇总数据(可选但推荐)
library(dplyr)
plot_data <- data %>% 
  count(Season, year, dir.binned, spd.binned, name = "count")

plot <- ggplot(data = plot_data, aes(x = dir.binned, y = count, fill = spd.binned)) +
  geom_col() + 
  cp + 
  facet_grid(Season ~year, scales = "free_y")  +
  theme(strip.text.x = element_text(size=8, angle=75),
        strip.text.y = element_text(size=12, face="bold"),
        strip.background = element_rect(colour= "white", fill= "#CCCCFF"))  +
  scale_x_discrete(drop = FALSE, labels = c("N", "NE", "E", "SE", "S", "SW", "W", "NW"))  +
  # 新增y轴配置,固定下限为0
  scale_y_continuous(limits = c(0, NA), expand = expansion(mult = c(0, 0.1))) +
  theme(text = element_text(size = 22))  +
  theme(panel.spacing = unit(0.5, "cm"))  +
  theme(axis.title.x = element_blank())  + 
  theme(axis.title.y = element_text(vjust = 4))  +
  theme(legend.position = "right")  +
  theme(plot.margin = unit(c(3,3,3,3), "cm"))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 22:18:01