一般情况下来说,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")
然后就成功了