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

基于日期统计量的折线图绘制报错求助(R语言ggplot2)

解决ggplot2绘制日期维度频率折线图的报错问题

你的代码存在两个关键错误:

  • aes(x=date())中调用的date()是R的系统日期函数,并非引用数据框里的date列,应直接使用列名date
  • stat_count用于自动统计分组频数,但你的数据已经有预计算好的Freq值,无需该统计层,直接用geom_line即可绘制折线

正确代码示例

首先确认数据结构(可选):

str(data_date)
# 预期输出:
# 'data.frame':	5 obs. of  2 variables:
#  $ date: Date, format: "2022-05-01" "2022-05-02" ...
#  $ Freq: int  200 300 400 500 600

绘制基础折线图:

library(ggplot2)
ggplot(data_date, aes(x = date, y = Freq)) +
  geom_line() +
  labs(x = "日期", y = "频率", title = "日期维度下频率变化趋势")

如果需要美化图表(调整样式、添加数据点):

ggplot(data_date, aes(x = date, y = Freq)) +
  geom_line(color = "steelblue", linewidth = 1.2) +
  geom_point(color = "darkred", size = 2) +
  theme_minimal() +
  labs(x = "日期", y = "频率", title = "日期维度下频率变化趋势")

内容的提问来源于stack exchange,提问作者Dr. Know Nothing

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 16:39:19