0

我第一次在 R 中使用 ggpattern 包,我无法让图像在我的情节中工作。我的代码的简化版本在这里:

x = seq(-1.5, 3.5, 0.1)

y = c( rep(1.0, 22), rep(0.2, 12), rep(0.7, 7), rep(1,10))

ref = data.frame(x = x, y = y)

library(dplyr)
library(ggplot2)
library(ggpattern)

coral = system.file("Coral", "coral.jpg", package="ggpattern")

p = ggplot(ref, aes(x = x, y = y))+
scale_y_reverse(lim = c(1, 0))+
theme_classic()+
geom_ribbon_pattern(aes(x = x, ymin = 1, ymax = y),
color = "darkblue",
fill = NA,
size = 1.5,
pattern = 'image',
pattern_type = 'squish',
pattern_filename = coral) +
geom_ribbon(aes(x = x, ymin = 0, ymax = y), fill = "lightblue")

当我运行此代码时,我得到的只是:Plot with missing image fill

在此处输入图像描述

但是有了这段代码,我认为曲线下的白色区域应该用珊瑚图像填充。有谁知道我在这里做错了什么?我搜索了 StackOverflow 和 GitHub,但找不到答案。

4

1 回答 1

0

我认为这里的问题是ggpattern的img文件夹中没有“coral.jpg”文件。

当我使用文件夹中存在的图像之一编辑您的代码时,它工作正常。

x = seq(-1.5, 3.5, 0.1)

y = c( rep(1.0, 22), rep(0.2, 12), rep(0.7, 7), rep(1,10))

ref = data.frame(x = x, y = y)

library(dplyr)
library(ggplot2)
library(ggpattern)

coral = system.file("img", "magpie.jpg", package="ggpattern")

p = ggplot(ref, aes(x = x, y = y))+
    scale_y_reverse(lim = c(1, 0))+
    theme_classic()+
    geom_ribbon_pattern(aes(x = x, ymin = 1, ymax = y),
                        color = "darkblue",
                        fill = NA,
                        size = 1.5,
                        pattern = 'image',
                        pattern_type = 'squish',
                        pattern_filename = coral) +
    geom_ribbon(aes(x = x, ymin = 0, ymax = y), fill = "lightblue")

p

在此处输入图像描述

在此处输入图像描述

于 2020-10-27T09:56:04.220 回答