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

如何为ggimage包的geom_image()单独设置图像尺寸并按实际比例绘图?

如何用geom_image()保留图像原始宽高比制作散点图

Got it, let's tackle how to keep the original aspect ratio of images when using geom_image() from the ggimage package—this is a super common pain point when trying to replicate those polished PPT-style scatter plots with properly scaled images. The core issue is that geom_image() will default to stretching images to fit a uniform size, so we need to manually calculate and apply each image's aspect ratio to fix this.

Step 1: Prepare your data with image dimensions

First, assuming you've already got your dataset loaded (like your brains data frame), we need to grab the width and height of each image to compute its aspect ratio. We'll use the magick package for this since it can read image metadata easily:

library(ggimage)
library(magick)
library(dplyr)
library(purrr) # For map() functions

# Your existing data loading code (adjust folder path as needed)
folder <- "path/to/your/images"
brains.dat <- file.path(folder, "brain-size.csv")
brains <- read.csv(brains.dat)

# Add image dimension and aspect ratio columns to your data frame
brains <- brains %>%
  mutate(
    # Read each image and extract its info
    img_obj = map(image_path, ~image_read(file.path(folder, .x))),
    img_info = map(img_obj, ~image_info(.x)),
    # Pull width and height values
    img_width = map_dbl(img_info, ~.$width),
    img_height = map_dbl(img_info, ~.$height),
    # Calculate aspect ratio (width / height)
    img_aspect = img_width / img_height
  )

Step 2: Plot with preserved aspect ratios

Now we'll use the img_aspect values to adjust the size parameter in geom_image(), and use the by argument to lock either the height or width as our scaling baseline:

ggplot(brains, aes(x = x_axis_column, y = y_axis_column)) +
  geom_image(
    aes(
      image = image_path,
      # Adjust size using the aspect ratio to preserve proportions
      # Here we set a base height of 5 (tweak this to fit your plot)
      size = I(5 / img_aspect)
    ),
    # Tell geom_image to scale images by their height (width will auto-adjust to match aspect ratio)
    by = "height"
  ) +
  theme_minimal()

Customization tips:

  • If you want to scale images by width instead of height, swap the by argument to "width" and adjust the size to I(5 * img_aspect) (where 5 is your base width).
  • If you want image sizes to correlate with a variable in your data (like brain size), combine that variable with the aspect ratio:
    ggplot(brains, aes(x = x_axis_column, y = y_axis_column)) +
      geom_image(
        aes(
          image = image_path,
          # Scale size relative to brain size, while preserving aspect ratio
          size = I((brain_size / max(brain_size)) * 10 / img_aspect)
        ),
        by = "height"
      ) +
      theme_minimal()
    

Key takeaways:

  • You must capture each image's original dimensions to calculate its aspect ratio—this is non-negotiable for preserving proportions.
  • The by argument controls whether we lock height or width as the scaling anchor, and the size parameter uses the aspect ratio to ensure the other dimension scales correctly.
  • Tweak the base size value (the 5 or 10 in the examples) to get the image sizes looking just right for your plot.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:33:20