首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ggplot2将图像添加到R图中,而to模式图像丢失了吗?

使用ggplot2将图像添加到R图中,而to模式图像丢失了吗?
EN

Stack Overflow用户
提问于 2020-10-27 03:52:25
回答 1查看 269关注 0票数 0

我是第一次在R中使用GggPatter包,我无法在我的情节中得到图像。下面是我的代码的一个简化版本:

代码语言:javascript
复制
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")

当我运行这段代码时,我所得到的就是:用缺少的图像填充来绘制

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-27 09:56:04

我认为这里的问题是在img模式的img文件夹中没有"coral.jpg“文件。

当我用文件夹中的一个图像编辑您的代码时,它工作得很好。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64548088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档