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

使用R语言ggplot绘图时无法提取马里奥原始及当前世界纪录问题求助

马里奥游戏世界纪录图表ggplot实现方案
  • 环境准备:需安装并加载tidyverse、lubridate两个R包,前者包含数据处理和绘图所需的dplyr、ggplot2组件,后者用于日期格式转换
install.packages(c("tidyverse", "lubridate")) # 首次使用先安装
library(tidyverse)
library(lubridate)
  • 数据读取与筛选
    首先将下载好的数据集存到本地,读取后筛选马里奥系列游戏的记录,分别标记每组的原始世界纪录(同游戏同速通类别下最早的记录)和当前世界纪录(同游戏同速通类别下最新的记录)
# 读取本地数据集,替换括号内为你自己的文件存储路径
mario_df <- read.csv("xxx/xxx/下载的数据集文件名.csv")

# 清洗筛选数据
wr_filtered <- mario_df %>%
  # 匹配所有名称含Mario的游戏记录
  filter(str_detect(game, fixed("Mario", ignore_case = TRUE))) %>%
  # 转换日期列格式为标准日期类型
  mutate(record_date = ymd(date)) %>%
  # 按游戏+速通类别分组
  group_by(game, category) %>%
  arrange(record_date) %>%
  mutate(
    # 标记组内第一条为原始WR
    is_original = row_number() == 1,
    # 标记组内最后一条为当前WR
    is_current = row_number() == n()
  ) %>%
  ungroup() %>%
  # 仅保留WR相关条目
  filter(is_original | is_current)
  • 绘图实现
    按照目标图样式,绘制原始WR到当前WR的连线,区分标注两种记录点:
ggplot(wr_filtered, aes(x = record_date, y = time, color = game)) +
  # 绘制同组原始WR到当前WR的连接线段
  geom_line(aes(group = interaction(game, category)), linewidth = 1, alpha = 0.7) +
  # 空心点标注原始WR
  geom_point(data = filter(wr_filtered, is_original), size = 3, shape = 1) +
  # 实心点标注当前WR
  geom_point(data = filter(wr_filtered, is_current), size = 3, shape = 16) +
  # 图表样式调整
  labs(
    title = "马里奥系列速通世界纪录演进",
    x = "记录创建日期",
    y = "速通时长",
    color = "游戏名称"
  ) +
  theme_bw() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold", size = 16),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

补充说明:如果数据集中time列是时分秒格式,需要先转为总秒数的数值类型再绘图;如果需要和目标图完全一致的配色,可以手动添加scale_color_manual()图层指定每个游戏对应的颜色值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 08:39:03