查看原文
其他


ggplot2已经非常好用了,只要掌握各种图层的细节,可以自定义各种好看的图形。ggtextggplot2的扩展包,可以让其支持markdown和HTML语法,让你的图形更好看!

安装

install.packages("ggtext")

remotes::install_github("wilkelab/ggtext")

theme中使用markdown语法

使用element_markdown()代替element_text()即可实现使用markdown语法渲染字体效果。

library(tidyverse)
## -- Attaching packages ----------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts -------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggtext)
library(glue)
## 
## 载入程辑包:'glue'
## The following object is masked from 'package:dplyr':
## 
##     collapse

data <- tibble(
  bactname = c("Staphylococcaceae""Moraxella""Streptococcus""Acinetobacter"),
  OTUname = c("OTU 1""OTU 2""OTU 3""OTU 4"),
  value = c(-0.50.523)
)

data %>% mutate(
  color = c("#009E73""#D55E00""#0072B2""#000000"),
  name = glue("<i style='color:{color}'>{bactname}</i> ({OTUname})"), # 使用css样式进行渲染
  name = fct_reorder(name, value)
)  %>%
  ggplot(aes(value, name, fill = color)) + 
  geom_col(alpha = 0.5) + 
  scale_fill_identity() +
  labs(caption = "Example posted on **stackoverflow.com**<br>(using made-up data)") +
  theme(
    axis.text.y = element_markdown(),
    plot.caption = element_markdown(lineheight = 1.2)
  )

使用图片作为横坐标标签

labels <- c(
  setosa = "<img src='https://gitee.com/ayue2019/drawing-bed/raw/master/setosa.jpg'
    width='100' /><br>*I. setosa*"
,
  virginica = "<img src='https://gitee.com/ayue2019/drawing-bed/raw/master/virginica.jpg'
    width='100' /><br>*I. virginica*"
,
  versicolor = "<img src='https://gitee.com/ayue2019/drawing-bed/raw/master/versicolor.jpg'
    width='100' /><br>*I. versicolor*"

)

ggplot(iris, aes(Species, Sepal.Width)) +
  geom_boxplot() +
  scale_x_discrete(
    name = NULL,
    labels = labels
  ) +
  theme(
    axis.text.x = element_markdown(color = "black", size = 11)
  )

使用HTML语法为标题添加各种效果

ggplot(mtcars, aes(disp, mpg)) + 
  geom_point() +
  labs(
    title = "<b>Fuel economy vs. engine displacement</b><br>
    <span style = 'font-size:10pt'>Lorem ipsum *dolor sit amet,*
    consectetur adipiscing elit, **sed do eiusmod tempor incididunt** ut
    labore et dolore magna aliqua. <span style = 'color:red;'>Ut enim
    ad minim veniam,</span> quis nostrud exercitation ullamco laboris nisi
    ut aliquip ex ea commodo consequat.</span>"
,
    x = "displacement (in<sup>3</sup>)",
    y = "Miles per gallon (mpg)<br><span style = 'font-size:8pt'>A measure of
    the car's fuel efficiency.</span>"

  ) +
  theme(
    plot.title.position = "plot",
    plot.title = element_textbox_simple(
      size = 13,
      lineheight = 1,
      padding = margin(5.55.55.55.5),
      margin = margin(005.50),
      fill = "cornsilk"
    ),
    axis.title.x = element_textbox_simple(
      width = NULL,
      padding = margin(4444),
      margin = margin(4000),
      linetype = 1,
      r = grid::unit(8"pt"),
      fill = "azure1"
    ),
    axis.title.y = element_textbox_simple(
      hjust = 0,
      orientation = "left-rotated",
      minwidth = unit(1"in"),
      maxwidth = unit(2"in"),
      padding = margin(4424),
      margin = margin(0020),
      fill = "lightsteelblue1"
    )
  )

为分面图的小标题添加各种效果

library(cowplot)

ggplot(mpg, aes(cty, hwy)) + 
  geom_point() +
  facet_wrap(~class) +
  theme_half_open(12) +
  background_grid() +
  theme(
    strip.background = element_blank(),
    strip.text = element_textbox(
      size = 12,
      color = "white", fill = "#5D729D", box.color = "#4A618C",
      halign = 0.5, linetype = 1, r = unit(5"pt"), width = unit(1"npc"),
      padding = margin(2010), margin = margin(3333)
    )
  )

主要是使用element_markdown()element_textbox()函数来添加各种元素,熟练使用,让你的图形更加美观!

不过需要了解一些csshtml的知识,感兴趣的小伙伴可以学习一下。



以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发

欢迎在评论区留言或直接添加我的微信!




欢迎关注我的公众号:医学和生信笔记

医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!


往期精彩内容:

R语言tidy风格医学统计学


R语言和医学统计学系列(11):球形检验


R语言和医学统计学系列(10):正态性和方差齐性检验


R语言和医学统计学系列(9):多重检验


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存