cowplot -- function referrence

65 篇文章 0 订阅

安装

install.packages("cowplot")
# or 
remotes::install_github("wilkelab/cowplot")

参考文档

https://www.rdocumentation.org/packages/cowplot/versions/1.1.1 ⇐ R documentation
https://wilkelab.org/cowplot/articles/introduction.html ⇐ introduction
https://wilkelab.org/cowplot/reference/index.html ⇐ function reference

Function reference

Theme function

  • 制造像 ggplot2’s theme_classic()一样的主题
library(ggplot2)
library(cowplot)

# classic cowplot theme
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + 
  geom_point() +
  theme_cowplot(12)

在这里插入图片描述

  • 其他的主题
# minimal grid theme
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + 
  geom_point() +
  theme_minimal_grid(12)

在这里插入图片描述

# minimal horizontal grid theme
ggplot(iris, aes(Sepal.Length, fill = Species)) + 
  geom_density(alpha = 0.5) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  theme_minimal_hgrid(12)

在这里插入图片描述

  • 参考主题
    在这里插入图片描述

Arranging plots or 组图

p1 <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point()
p2 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point()

plot_grid(p1, p2, labels = c('A', 'B'), label_size = 12)

# 参考文档:https://wilkelab.org/cowplot/reference/plot_grid.html

在这里插入图片描述

图片对齐

library(cowplot)
df <- data.frame(
  x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3
)

p1 <- ggplot(df, aes(x, y1)) + geom_point() + theme(axis.text.y = element_text(size = 14))
p3 <- ggplot(df, aes(x, y3)) + geom_point() + theme(axis.text.y = element_text(size = 28))

plot_grid(p1,p3,ncol=1)

在这里插入图片描述

# 当你 使用align参数时,plot_grid会调用 align_plots函数,最终使得图片的某个坐标轴对齐
plot_grid(p1,p3,ncol=1,axis = "lr",align = "hv")

在这里插入图片描述

  • 手动使图片对齐
p1 <- ggplot(mpg, aes(manufacturer, hwy)) + stat_summary(fun="median", geom = "bar") +
  theme_half_open() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust= 1))
p2 <- ggplot(mpg, aes(manufacturer, displ)) + geom_point(color="red") +
scale_y_continuous(position = "right") +
  theme_half_open() + theme(axis.text.x = element_blank())

plot_grid(p1,p2,ncol = 1)

在这里插入图片描述

# manually align and plot on top of each other
aligned_plots <- align_plots(p1, p2, align="hv", axis="tblr")

# Note: In most cases two y-axes should not be used, but this example
# illustrates how one could accomplish it.
ggdraw(aligned_plots[[1]]) + draw_plot(aligned_plots[[2]])

在这里插入图片描述

图中图

p <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point(size = 1.5, color = "blue") +
  theme_cowplot(12)

logo_file <- system.file("extdata", "logo.png", package = "cowplot")

# ggdraw() takes a snapshot of the plot and places it at full size into a new drawing canvas
# draw_image() then draws an image on top of the plot
ggdraw(p) + 
  draw_image(logo_file, x = 1, y = 1, hjust = 1, vjust = 1, width = 0.13, height = 0.2)

在这里插入图片描述

# ggdraw  & draw_image &  draw_plot 的组合顺序不同会有不同的效果
#   ggdraw()  first setting up an empty drawing canvas
#  draw_image  then drawing the image
#  draw_plot(p)  and then drawing the plot on top
ggdraw() + 
  draw_image(logo_file, scale = 0.5) +
  draw_plot(p)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值