参考:https://mp.weixin.qq.com/s/c2SfPzGyjKZ3lPOdq6JVmg 注明:仅作为笔记,无任何商业用途。

介绍

有很多R主题包可以方便的进行图形主题修改,但是这里学习下如何自定义自己的绘图主题,下面关键一些主题函数已经进行了注释,可以复制代码跑一跑。

ggplot2主题

Components

Components指图上的y轴标题,x轴的ticks等,只要你能在图上看见的部分,其包括:

  1. aspect.ratio
  2. axis.title, axis.title.x, axis.title.x.top, axis.title.x.bottom,
  3. axis.title.y, axis.title.y.left, axis.title.y.right, axis.text,
  4. axis.text.x, axis.text.x.top, axis.text.x.bottom, axis.text.y,
  5. axis.text.y.left, axis.text.y.right, axis.ticks, axis.ticks.x,
  6. axis.ticks.x.top, axis.ticks.x.bottom, axis.ticks.y,
  7. axis.ticks.y.left, axis.ticks.y.right, axis.ticks.length,
  8. axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom,
  9. axis.line.y, axis.line.y.left, axis.line.y.right
  10. legend.background, legend.margin, legend.spacing, legend.spacing.x,
  11. legend.spacing.y, legend.key, legend.key.size, legend.key.height,
  12. legend.key.width, legend.text, legend.text.align, legend.title,
  13. legend.title.align, legend.position, legend.direction,
  14. legend.justification, legend.box, legend.box.just

Elements

Elements:各个Components有各种各样的Elements,理解为Components的样式,设定Components的展现形式,包括:

  1. element_line(), element_rect()
  2. element_text(), element_blank()

实战

环境准备

  1. # 环境准备
  2. install.packages("tidyverse")
  3. install.packages("gapminder")
  4. library(tidyverse)
  5. library(gapminder)

生成测试数据

  1. # 示例数据构建
  2. gapminder %>%
  3. filter(year == 1992) %>%
  4. ggplot() +
  5. aes(x = gdpPercap, y = lifeExp) +
  6. geom_point(alpha = 0.8, shape = 21,
  7. fill = "white") +
  8. aes(col = continent, fill = continent) +
  9. geom_point(alpha = 0.3, shape = 21) +
  10. aes(size = pop) +
  11. scale_size(guide = F) +
  12. labs(title = "Wealth and expected longevity\n in 1992") +
  13. labs(subtitle = "Data from gapminder package in R") +
  14. labs(x = "GDP per Capita") +
  15. labs(y = "Life Expectency") + # a tag is also an option
  16. labs(col = "Continent", fill = "Continent") +
  17. labs(caption = "Vis: @EvaMaeRey with ggplot") ->
  18. g

Components在图形中的位置

  1. # components在图形中的位置
  2. g +
  3. # 图形四周
  4. theme(rect = element_rect(fill = "snow3")) +
  5. # 字体
  6. theme(text = element_text(family = "Times New Roman")) +
  7. # x,y轴标题
  8. theme(axis.title = element_text(color = "slateblue4")) +
  9. # 标题
  10. theme(title = element_text(color = "seagreen")) +
  11. # x,y轴线条
  12. theme(axis.line = element_line(color = "orange")) +
  13. # 图例
  14. theme(legend.key = element_rect(fill = "violet")) +
  15. # x,y轴ticks
  16. theme(axis.ticks = element_line(color = "blue")) +
  17. # x轴ticks
  18. theme(axis.ticks.x = element_blank()) +
  19. # 图形背景
  20. theme(panel.background = element_rect(fill = "steelblue")) +
  21. # 图形网格线条
  22. theme(panel.grid = element_line(size = 3, color = "thistle1")) +
  23. # 图例背景
  24. theme(legend.background = element_rect(fill = "goldenrod1")) +
  25. # 图例字体
  26. theme(legend.text = element_text(size = 7)) +
  27. # 图例位置
  28. theme(legend.position = "bottom") +
  29. # 图例边缘
  30. theme(legend.margin = margin(t = 0, r = 20,
  31. b = 0, l = 40, unit = "pt")) +
  32. # 图形长宽比例
  33. theme(aspect.ratio = .5)

element_text调整字体

  1. # elements_text调整字体
  2. g +
  3. # 大标题位置
  4. theme(plot.title.position = "plot") +
  5. # 大标题高度-lineheight
  6. theme(plot.title = element_text(color = "plum4",
  7. size = 20,
  8. lineheight = 2.5)) +
  9. # 小标题颜色
  10. theme(plot.subtitle = element_text(color = "plum4")) +
  11. # y轴标题
  12. theme(axis.title.y = element_text(color = "plum4",
  13. size = 20 )) +
  14. # y轴上字体颜色
  15. theme(axis.text.y = element_text(color = "plum4")) +
  16. # 图例颜色
  17. theme(legend.title = element_text(color = "plum4",
  18. size = 22)) +
  19. # 图例字体颜色
  20. theme(legend.text = element_text(color = "plum4")) +
  21. # x轴标题颜色
  22. theme(axis.title.x = element_text(color = "plum4")) +
  23. # x轴字体颜色
  24. theme(axis.text.x = element_text(color = "plum4")) +
  25. # caption注释颜色
  26. theme(plot.caption = element_text(color = "plum4",
  27. size = 20)) +
  28. # 图形中所有字体,定义字体类型
  29. theme(text = element_text(family = "Times")) +
  30. # 所有字体样式
  31. theme(text = element_text(face = "bold.italic")) +
  32. # 所有字体间的间隔
  33. theme(text = element_text(margin = margin(t = 3, r = 3, b = 3, l = 3,
  34. unit = "pt"))) +
  35. # 字体旋转角度
  36. theme(text = element_text(angle = 2)) +
  37. # 大标题字体位置
  38. theme(plot.title = element_text(hjust = .5)) +
  39. # x轴标题字体位置
  40. theme(axis.title.x = element_text(hjust = 1))

element_rect调整图形背景

  1. # element_rect调整图形背景
  2. g +
  3. # 图形四周背景颜色
  4. theme(plot.background = element_rect(fill = "purple",
  5. color = "darkolivegreen",
  6. size = 3)) +
  7. # 图形背景颜色
  8. theme(panel.background = element_rect(fill = "purple",
  9. color = "darkolivegreen",
  10. size = 3)) +
  11. # 图例背景颜色
  12. theme(legend.background = element_rect(fill = "purple",
  13. color = "darkolivegreen",
  14. size = 3)) +
  15. # 图例中的图形背景颜色
  16. theme(legend.key = element_rect(fill = "purple",
  17. color = "darkolivegreen",
  18. size = 3)) +
  19. # 图形边框颜色
  20. theme(panel.border = element_rect(color = "magenta",
  21. fill = NA, size = 4))

element_line调整线条

  1. # element_line调整线条
  2. g +
  3. # y轴线体颜色
  4. theme(axis.ticks.y = element_line(size = 10,
  5. color = "blue")) +
  6. # x轴线体颜色
  7. theme(axis.ticks.x = element_line(size = 10,
  8. color = "blue")) +
  9. # 背景大横线颜色
  10. theme(panel.grid.major.y = element_line(size = 2,
  11. linetype = "dashed",
  12. color = "blue",
  13. lineend = "round")) +
  14. # 背景大竖线颜色
  15. theme(panel.grid.major.x = element_line(size = 2,
  16. arrow = arrow(),
  17. color = "blue",
  18. lineend = "round")) +
  19. # 背景小横线颜色
  20. theme(panel.grid.minor.y = element_line(size = 1,
  21. linetype = "dotted",
  22. color = "blue",
  23. lineend = "butt")) +
  24. # 背景网格线颜色
  25. theme(panel.grid = element_line(color = "blue")) +
  26. # unknow
  27. theme(axis.line.x.top = element_line(size = 2,
  28. color = "blue")) +
  29. # 去除背景颜色
  30. theme(panel.ontop = TRUE,
  31. panel.background = element_blank()) # in front of the data

哪些aesthetics(图上映射的形状)的elements是可以调整的?

image.png

图例调整

  1. # 图例调整
  2. g +
  3. # 图例位置:下
  4. theme(legend.position = "bottom") +
  5. # 图例:去除
  6. theme(legend.position = "none") +
  7. # 图例位置:x=0.8 y=0.25
  8. theme(legend.position = c(.8, .25)) +
  9. # 图例水平放置
  10. theme(legend.direction = "horizontal") +
  11. # 去除图例中的图形
  12. theme(legend.key = element_rect()) +
  13. guides(fill = guide_legend(size = 35))

其他修改

  1. # 其它调整
  2. g +
  3. # unknow
  4. theme(plot.tag.position = "topright") +
  5. # 去除颜色图例
  6. scale_color_discrete(guide = FALSE) +
  7. # 去除填充图例
  8. scale_fill_discrete(guide = FALSE) +
  9. # x轴放于上方
  10. scale_x_continuous(position = "top") +
  11. # y轴放于右侧
  12. scale_y_continuous(position = "right") +
  13. # 长宽比:0.5
  14. theme(aspect.ratio = .5)

分面的主题

  1. # 分面主题
  2. g +
  3. # facet_wrap进行分面
  4. facet_wrap(~ continent,
  5. strip.position = "right") +
  6. # 分面的分组字体背景
  7. theme(strip.background =
  8. element_rect(fill = "pink")) +
  9. # 分面的分组字体颜色
  10. theme(strip.text.y =
  11. element_text(color = "snow", size = 12)) +
  12. # 分面的分组字体旋转角度
  13. theme(strip.text.y =
  14. element_text(angle = -80)) +
  15. # 各个分面在x轴方向的间隔
  16. theme(panel.spacing.x =
  17. unit(1.5, "lines")) +
  18. # 各个分面在y轴方向的间隔
  19. theme(panel.spacing.y =
  20. unit(2, "lines")) +
  21. theme(strip.switch.pad.grid =
  22. unit(0, "cm"))