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

在R中创建直方图时遭遇fortify()报错问题

问题解决

错误原因

  1. 未指定数据集:ggplot()函数必须通过data参数明确传入要使用的数据集,你的代码仅定义了aes()映射,但未告知ggplot数据来源,导致它无法识别数据类型,触发fortify()错误。
  2. 几何对象使用不当:geom_histogram()用于展示单个变量的分布,同时映射x=party_label和y=nominate_dim1不符合它的使用逻辑,需根据实际需求更换合适的几何对象。

修正后的代码

场景1:按党派分组展示维度估计值的分布(推荐用箱线图)

q1 <- Hsall_df %>% filter(code == 118)
ggplot(data = q1, aes(x = party_label, y = nominate_dim1)) +
  geom_boxplot() +
  labs(x = "Party", y = "First Dimension Estimate") +  # 修正拼写错误:Esitmate → Estimate
  theme_bw()

场景2:按党派分开展示维度估计值的直方图

q1 <- Hsall_df %>% filter(code == 118)
ggplot(data = q1, aes(x = nominate_dim1, fill = party_label)) +
  geom_histogram(position = "dodge", bins = 15) +
  labs(x = "First Dimension Estimate", y = "Count", fill = "Party") +
  theme_bw()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 16:52:02