ggplot2显示png

一般情况下来说,ggplot2画的图是我们ggplot自己建立起来的对象,但是有的时候,为了排版的美观,我需要从外地导入一张图片从而和其他ggplot对象一起进行操作,操作如下

案例1

library(ggplot2)
library(cowplot)
library(magick)
library(patchwork)
# Update 2020-04-15: 
# As of version 1.0.0, cowplot does not change the default ggplot2 theme anymore.
# So, either we add theme_cowplot() when we build the graph 
# (commented out in the example below), 
# or we set theme_set(theme_cowplot()) at the beginning of our script:
theme_set(theme_cowplot())

my_plot <- 
  ggplot(data    = iris, 
         mapping = aes(x    = Sepal.Length, 
                       fill = Species)) + 
  geom_density(alpha = 0.7) # +
  # theme_cowplot()

# Example with PNG (for fun, the OP's avatar - I love the raccoon)
p1=ggdraw() +
  draw_image("./dog.jpeg") 

p2=ggdraw() +
  draw_image("./dog.jpeg") 

p1+p2

结果如下
在这里插入图片描述

案例2

library(ggplot2)
library(cowplot)
library(magick)
library(patchwork)
# Update 2020-04-15: 
# As of version 1.0.0, cowplot does not change the default ggplot2 theme anymore.
# So, either we add theme_cowplot() when we build the graph 
# (commented out in the example below), 
# or we set theme_set(theme_cowplot()) at the beginning of our script:
theme_set(theme_cowplot())

my_plot <- 
  ggplot(data    = iris, 
         mapping = aes(x    = Sepal.Length, 
                       fill = Species)) + 
  geom_density(alpha = 0.7) # +
  # theme_cowplot()

# Example with PNG (for fun, the OP's avatar - I love the raccoon)
p1=ggdraw() +
  draw_image("./dog.jpeg") 

p2=ggdraw() +
  draw_image("./test_sanky.png") 
p1+p2

在这里插入图片描述

案例3

library(ggplot2)
library(cowplot)
library(magick)
library(patchwork)
# Update 2020-04-15: 
# As of version 1.0.0, cowplot does not change the default ggplot2 theme anymore.
# So, either we add theme_cowplot() when we build the graph 
# (commented out in the example below), 
# or we set theme_set(theme_cowplot()) at the beginning of our script:
theme_set(theme_cowplot())

my_plot <- 
  ggplot(data    = iris, 
         mapping = aes(x    = Sepal.Length, 
                       fill = Species)) + 
  geom_density(alpha = 0.7) # +
  # theme_cowplot()

# Example with PNG (for fun, the OP's avatar - I love the raccoon)
p1=ggdraw() +
  draw_image("./dog.jpeg") 

p2=ggdraw() +
  draw_image("./test_sanky.png",scale = 2) 

p1+p2

结果如下
在这里插入图片描述

这里顺便记录一下在linux ubuntu上安装

首先

sudo apt-get install -y libmagick++-dev

然后进入conda的R环境,

install.packages("magick")

但是这里出错了
在这里插入图片描述解决办法
在这里插入图片描述
然后再次

install.packages("magick")

然后就成功了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值