可视化

R-geomtextpath 简介

在常见的绘图过程中,一般的文本绘制都是短的、笔直的并且与轴一致。但有时弯曲的文本注释在绘图过程中也十分重要,特别是在极坐标系下的一些图表绘制。R-geomtextpath包就是R语言中实现弯曲文本注释功能的优秀第三方包,且基于ggplot2绘图体系,学习成本大大降低。

R-geomtextpath 示例样式

样例一:geom_textpath()

  1. t <- seq(5, -1, length.out = 1000) * pi
  2. spiral <- data.frame(
  3. x = sin(t) * 1000:1,
  4. y = cos(t) * 1000:1
  5. )
  6. rhyme <- paste(
  7. "Like a circle in a spiral, like a wheel within a wheel,",
  8. "never ending or beginning on an ever spinning reel")
  9. p <- ggplot(spiral, aes(x, y, label = rhyme)) +
  10. coord_equal(xlim = c(-1000, 1000), ylim = c(-1000, 1000))
  11. p + geom_textpath(size = 4)

Example Of geom_textpath()

样例二:geom_labelpath()

  1. p + geom_labelpath(size = 4, textcolour = "#E41A1C", boxcolour = "#377eb8",
  2. fill = "#ffff99", boxlinetype = "dotted", linewidth = 1,
  3. boxlinewidth = 0.5) +
  4. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")

Example Of geom_labelpath()

样例三:geom_textdensity()

  1. i <- ggplot(iris, aes(x = Sepal.Length, colour = Species, label = Species)) +
  2. ylim(c(0, 1.3))
  3. i + geom_textdensity(size = 5, vjust = -0.2, hjust = "ymax") +
  4. labs(subtitle = "hjust = \"ymax\"")+
  5. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")

Example Of geom_textdensity()

样例四:coord_curvedpolar()

  1. df <- data.frame(Temperature = c(4.4, 4.6, 6.3, 8.7, 11.6, 14.1, 15.9, 15.5,
  2. 13.1, 9.7, 6.7, 4.3, 3.6, 3.9, 6.4, 9.7, 13.2,
  3. 15.8, 18, 17.8, 15.1, 11.2, 7.2, 4.4),
  4. City = rep(c("Glasgow", "Amsterdam"), each = 12),
  5. Month = factor(rep(month.name, 2), month.name))
  6. p <- ggplot(df, aes(Month, Temperature)) +
  7. geom_col(aes(fill = City), position = position_dodge(width = 1)) +
  8. geom_vline(xintercept = 1:13 - 0.5, color = "gray90") +
  9. geom_hline(yintercept = 0:3 * 5, color = "gray90") +
  10. scale_fill_manual(values = c("darkorange", "dodgerblue4")) +
  11. ggtitle("Average monthly temperature in Amsterdam and Glasgow") +
  12. theme_bw() +
  13. theme(panel.border = element_blank(),
  14. axis.text.x = element_text(size = 14),
  15. axis.title.x = element_blank(),
  16. panel.grid.major = element_blank())
  17. p + coord_curvedpolar()

Example Of coord_curvedpolar()

样例五:Stat layers 绘制

  1. ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) +
  2. geom_point(alpha = 0.3) +
  3. stat_ellipse(
  4. aes(label = Species),
  5. geom = "textpath", hjust = 0.25,
  6. ) +
  7. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  8. theme(legend.position = "none")

Example Of Stat layers

样例六:Covid effects on life activities

  1. ggplot(covidpie$df, aes(xvals, yvals)) +
  2. geom_col(width = 1.05, aes(fill = cols)) +
  3. geom_vline(colour = "white", xintercept = sep, size = 3) +
  4. geom_segment(data = data.frame(x = 0.5 + 1:24, y = 0, yend = 1),
  5. aes(x = x, y = 0, yend = 1, xend = x), colour = "white",
  6. inherit.aes = FALSE) +
  7. geom_textpath(data = covidpie$sublabels,
  8. mapping = aes(x, y, label = z, group = id), size = 3.8,
  9. upright = FALSE, text_only = TRUE, hjust = 0,
  10. color = "white", family = "Times New Roman") +
  11. geom_text(x = 32, y = -2, label = "Activities", check_overlap = TRUE,
  12. size = 7, family = "Times New Roman") +
  13. geom_text(x = 32, y = -1.4, label= "Changing time allocations\ndue to Covid",
  14. lineheight = 1, check_overlap = TRUE, family = "Times New Roman") +
  15. geom_textpath(data = covidpie$df_text, aes(x, y, label = text),
  16. upright = FALSE, size = 2, rich = TRUE, text_only = TRUE,
  17. halign = "left") +
  18. geom_text(x = 36.5, y = 1.8, label = covidpie$label1, hjust = 0,
  19. size = 2, check_overlap = TRUE, vjust = 1) +
  20. geom_text(x = 32.8, y = 0.75, label = covidpie$label2, vjust = 1, hjust = 0,
  21. size = 2, check_overlap = TRUE) +
  22. geom_text(x = 28.8, y = 1.04, label = covidpie$label3, hjust = 0,
  23. size = 2, check_overlap = TRUE, vjust = 1) +
  24. scale_y_continuous(limits = c(-2, 2.2)) +
  25. scale_fill_manual(values = covidpie$fills) +
  26. scale_x_continuous(expand = c(0.2, 1)) +
  27. coord_polar(start = -pi) +
  28. theme_void() +
  29. theme(legend.position = "none")

绘图技巧 | 文本弯曲可视化技巧 - 图6

样例七:Planetary orbits

  1. set.seed(1)
  2. planets <- c("Jupiter", "Saturn", "Uranus", "Neptune")
  3. t <- seq(0, pi * 2, len = 500)
  4. AU <- c(5.203, 9.539, 19.18, 30.06)
  5. df <- data.frame(x = as.vector(outer(cos(t), AU)),
  6. y = as.vector(outer(sin(t), AU)),
  7. Planet = rep(planets, each = 500),
  8. position = rep(c(0.75, 0.2, 0.4, 0.1)))
  9. ggplot(df, aes(x, y, color = Planet)) +
  10. geom_point(inherit.aes = FALSE,
  11. data = data.frame(x = runif(1000, -30, 30),
  12. y = runif(1000, -30, 30),
  13. intens = runif(1000)/2),
  14. mapping = aes(x, y, alpha = intens), color = "white", size = 0.2) +
  15. geom_textpath(aes(label = Planet), linewidth = 0.3,
  16. hjust = rep(c(0, 0.25, 0.75, 1), each = 500),
  17. vjust = 1.1, size = 5, upright = TRUE, fontface = 2) +
  18. geom_point(data = df[c(170, 600, 1385, 1965),], size = c(4, 3.5, 1.5, 1.5)) +
  19. geom_point(x = 0, y = 0, size = 5, color = "#FFFFA0", inherit.aes = FALSE) +
  20. scale_alpha_identity() +
  21. scale_color_manual(values = c(Jupiter = "orangered",
  22. Uranus ="violet",
  23. Neptune = "skyblue",
  24. Saturn = "gold")) +
  25. coord_equal() +
  26. theme_void() +
  27. theme(plot.background = element_rect(fill = "black"),
  28. legend.position = "none")

绘图技巧 | 文本弯曲可视化技巧 - 图7