如何使用ggpattern将图像添加到直方图中?

How to add images to the histogram using ggpattern?

提问人:QMan5 提问时间:11/11/2023 最后编辑:QMan5 更新时间:11/11/2023 访问量:49

问:

我正在尝试使用 ggpattern 将图像添加到直方图中,但我似乎无法让它工作。

library(ggplot2)
library(ggpattern)

image_paths <- c("img1.png", "20190530153216.png", "download.png", "dstack2.png", "Dstack091.png", "Rodząca_dafnia.png")

# Sample dataframe with values
df <- data.frame(
  example_column = rnorm(32)  # Replace with your actual data
)

# Create a histogram with images as fill patterns


p <- ggplot(df, aes(x = example_column)) +
  geom_histogram_pattern(pattern = "image", aes(pattern_filename = image_paths),
                         pattern_type = "squish") +
  scale_pattern_filename_identity() +
  labs(x = "Residual Value", y = "Count") +
  theme_minimal()

# Display the histogram
print(p)

我收到以下错误:

Error in `geom_histogram_pattern()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (32)
✖ Fix the following mappings: `pattern_filename`

我试图添加,但也没有用:scale_pattern_filename_manual()

img1 <- system.file("img", "img1.jpg"   , package="ggpattern")
img2 <- system.file("img", "20190530153216.jpg"   , package="ggpattern")
img3 <- system.file("img", "download.jpg"   , package="ggpattern")
img4 <- system.file("img", "dstack2.jpg"   , package="ggpattern")
img5 <- system.file("img", "Dstack091.jpg"   , package="ggpattern")
img6 <- system.file("img", "Rodząca_dafnia.jpg"   , package="ggpattern")

p <- ggplot(df, aes(x = example_column)) +
  geom_histogram_pattern(pattern = "image",
                         pattern_type = "squish", bins = 6) +
  scale_pattern_filename_manual(values = c(`1` = img1, `2` = img2, `3` = img3, `4` = img4 , `5` =img5 , `6`=img6)) +
  labs(x = "Residual Value", y = "Count") +
  theme_minimal()
r ggplot2 直方图

评论

0赞 Axeman 11/11/2023
我想你需要,看 coolbutuseless.github.io/package/ggpattern/articles/......scale_pattern_filename_manual
0赞 MrFlick 11/11/2023
可能的重复:stackoverflow.com/questions/77461483/...在此数据中指定模式文件名的位置?你有,但没有在任何地方定义aes(pattern_filename = image_paths)image_paths
0赞 QMan5 11/11/2023
不,那个是用于条形图的。

答:

2赞 stefan 11/11/2023 #1

正如艾伦·卡梅伦(Allan Cameron)在回答您之前的问题时已经指出的那样,您必须绘制地图。否则将不起作用。但是,在这种情况下有点特别。基本上,我们需要映射可以使用 实现的 bin 索引。其次,当然,您是否需要传递有效图像文件路径的向量(例如 不是)的参数。在下面的代码中,我只使用了 R 徽标。pattern_filenamescale_pattern_filename_xxxgeom_histogrampattern_filenameafter_stat()system.file("img", "Rodząca_dafnia.jpg", package="ggpattern")values=scale_pattern_filename_manual

library(ggplot2)
library(ggpattern)

set.seed(123)

ggplot(df, aes(x = example_column)) +
  geom_histogram_pattern(
    aes(
      pattern_filename = after_stat(
        as.character(seq_along(x))
      )
    ),
    pattern = "image",
    pattern_type = "squish",
    bins = 6
  ) +
  scale_pattern_filename_manual(
    values = rep("https://www.r-project.org/logo/Rlogo.png", 6),
    guide = "none"
  ) +
  labs(x = "Residual Value", y = "Count") +
  theme_minimal()