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

Kaggle中ggimage包geom_image的替代方案:柱状图顶部添加电影图片

柱状图顶部添加电影图片的替代方案(Kaggle环境)

问题背景

处理电影数据集时,需绘制柱状图并在每个柱子顶部添加对应电影海报。本地用ggimage的geom_image可实现,但Kaggle不支持该包,需寻找替代方法。

方法1:使用grid包手动定位添加图片

步骤说明

  1. 将在线图片下载到Kaggle工作目录
  2. 绘制基础柱状图
  3. 通过grid包加载图片,定位到对应柱子顶部

代码实现

# 加载所需包
library(data.table)
library(ggplot2)
library(grid)
library(png)

# 确保top_voted数据集已准备好(含title、vote_count、image_url字段)
# 下载图片到本地工作目录,替换空格避免路径问题
top_voted[, img_path := paste0(gsub(" ", "_", title), ".jpg")]
for(i in 1:nrow(top_voted)){
  download.file(top_voted$image_url[i], destfile = top_voted$img_path[i], mode = "wb", quiet = TRUE)
}

# 绘制基础柱状图
base_plot <- ggplot(data = top_voted, aes(
  x = reorder(title, -vote_count),
  y = vote_count,
  fill = title
)) +
  geom_bar(stat = "identity") +
  labs(fill = "电影名称",
       title = "投票数Top10动画电影",
       x = "电影名称",
       y = "投票数") +
  theme(axis.text = element_text(size = 12, angle = 54, hjust = 1),
        plot.title = element_text(size = 16, hjust=0.5))

# 渲染基础图
print(base_plot)

# 获取每个柱子的x轴坐标位置
x_positions <- ggplot_build(base_plot)$data[[1]]$x
max_votes <- max(top_voted$vote_count)

# 逐个添加图片到柱子顶部
for(i in 1:nrow(top_voted)){
  # 加载图片并转为grid对象,调整显示大小
  img_obj <- rasterGrob(readPNG(top_voted$img_path[i]), width = 0.07, height = 0.07)
  # 计算图片的y轴位置(柱子顶部+偏移量)
  y_position <- (top_voted$vote_count[i] + 400) / max_votes * 0.9
  # 定位并绘制图片
  pushViewport(viewport(x = x_positions[i]/length(x_positions), y = y_position, just = "center"))
  grid.draw(img_obj)
  popViewport()
}

方法2:使用ggplot2的annotate结合magick处理图片

步骤说明

  1. 用magick包读取在线图片并调整尺寸
  2. 将图片转为raster对象
  3. 通过annotate函数逐个添加到对应柱子顶部

代码实现

# Kaggle环境首次运行需安装magick包
# install.packages("magick")
library(magick)
library(ggplot2)
library(data.table)

# 处理图片:读取在线图片并转为raster对象,调整尺寸
top_voted[, img_raster := lapply(image_url, function(url){
  image_read(url) %>% 
    image_resize("80x80") %>%
    as.raster()
})]

# 绘制基础柱状图
base_plot <- ggplot(data = top_voted, aes(
  x = reorder(title, -vote_count),
  y = vote_count,
  fill = title
)) +
  geom_bar(stat = "identity") +
  labs(fill = "电影名称",
       title = "投票数Top10动画电影",
       x = "电影名称",
       y = "投票数") +
  theme(axis.text = element_text(size = 12, angle = 54, hjust = 1),
        plot.title = element_text(size = 16, hjust=0.5))

# 获取x轴的分类级别
x_levels <- levels(reorder(top_voted$title, -top_voted$vote_count))

# 逐个添加图片注释,调整位置和大小
for(i in 1:nrow(top_voted)){
  base_plot <- base_plot + 
    annotate("raster", 
             x = x_levels[i], 
             y = top_voted$vote_count[i] + 400,
             width = 0.7, height = 1200,
             raster = top_voted$img_raster[[i]])
}

# 渲染最终图
print(base_plot)

注意事项

  • 可根据实际需求调整图片大小(width/height)和y轴偏移量(+400),确保图片位置合适
  • 下载图片时若遇到URL失效,需替换为有效海报链接

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 16:14:53