可视化 R
在绘制可视化图表时经常需要对特定区域、位置等使用文本或箭头等标识性字符进行注释显示,这种注释在可视化制作中尤为重要,它可以突出重要信息,引起人们对图形某个特征的关注。接下来就汇总一下在R和Python可视化绘制中是如何进行注释的。具体内容如下:

  • R注释操作
  • Python注释操作

    R注释操作

    在使用R进行可视化绘制中,起注释作用的绘图函数有很多,这里还是介绍基于ggplot2绘图体系中的绘图函数,主要介绍R-ggplot2和R-ggforce 包中关于注释的内容,如下:

    R-ggplot2 注释操作

    这一部分使用ggplot2中annotate()函数进行说明,这里直接给出一个具体案例,如下: ```r library(tidyverse) library(ggtext) library(hrbrthemes) library(ggpubr) library(ggsci) library(ggforce)

plot01 <- ggplot(data = iris,aes(Petal.Length, Petal.Width, )) + geom_point(shape=21,aes(fill=Species),colour=”black”,size=3) + scale_fill_jco()+

基础注释方式

annotate( geom = “curve”, x = 2., y = 1, xend = 1.5, yend = .65, curvature = .3,arrow = arrow(length = unit(2, “mm”)))+ annotate(geom = “text”, x = 2.1, y = 1, label = “setosa”, hjust=”left”,vjust = .5)+

labs( title = “Example of ggplot2::annotate()“, subtitle = “processed charts with annotate()“, caption = “Visualization by DataCharm“) + hrbrthemes::theme_ipsum(base_family = “Roboto Condensed”) + theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = “black”, size = 20, margin = margin(t = 1, b = 12)), plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15), plot.caption = element_markdown(face = ‘bold’,size = 12), )

  1. ![](https://cdn.nlark.com/yuque/0/2021/webp/396745/1624341260115-adbd04c7-ca29-43e6-9492-35fe0b0d42d1.webp#clientId=u72f79dfb-db95-4&from=paste&id=u413fe4fc&margin=%5Bobject%20Object%5D&originHeight=810&originWidth=1080&originalType=url&ratio=3&status=done&style=shadow&taskId=u3a1efd7c-e667-4684-8b47-a31513bca77)<br />Example of ggplot2 annotate()<br />当然如果想要实现这种“箭头”效果,ggplot2的`geom_segment()`和`geom_curve()`都可实现,感兴趣的小伙伴可去ggplot2官网([https://ggplot2.tidyverse.org/reference/index.html](https://ggplot2.tidyverse.org/reference/index.html)) 进行探索。下面介绍一种更为方便直观且简单的方法。
  2. <a name="dHKlU"></a>
  3. ### R-ggforce 注释操作
  4. R-ggforce包中有几个绘图函数可以实现较为灵活的注释效果,且语法较为简单。官网为:[https://ggforce.data-imaginist.com/reference/index.html](https://ggforce.data-imaginist.com/reference/index.html)。详细如下:
  5. <a name="ICtN4"></a>
  6. #### 「geom_mark_rect()」
  7. ```r
  8. ggplot(iris, aes(Petal.Length, Petal.Width)) +
  9. geom_mark_rect(aes(fill = Species, label = Species),
  10. con.cap = 0,label.fill='gray',
  11. label.colour="black") +
  12. geom_point(shape=21,aes(fill=Species),colour="black",size=3) +
  13. scale_fill_nejm() +
  14. labs(
  15. title = "Example of <span style='color:#D20F26'>ggforce::geom_mark_rect()</span>",
  16. subtitle = "processed charts with <span style='color:#1A73E8'>geom_mark_rect()</span>",
  17. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  18. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  19. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  20. size = 20, margin = margin(t = 1, b = 12)),
  21. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  22. plot.caption = element_markdown(face = 'bold',size = 12),
  23. )

绘图技巧 | 绘图显著标明技巧 - 图1
Example of ggforce::geom_mark_rect()

geom_mark_circle()

  1. ggplot(iris, aes(Petal.Length, Petal.Width)) +
  2. geom_mark_circle(aes(fill = Species, label = Species),
  3. con.cap = 0,label.fill='gray',
  4. label.colour="black") +
  5. geom_point(shape=21,aes(fill=Species),colour="black",size=3) +
  6. scale_fill_nejm() +
  7. labs(
  8. title = "Example of <span style='color:#D20F26'>ggforce::geom_mark_circle()</span>",
  9. subtitle = "processed charts with <span style='color:#1A73E8'>geom_mark_circle()</span>",
  10. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  11. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  12. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  13. size = 20, margin = margin(t = 1, b = 12)),
  14. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  15. plot.caption = element_markdown(face = 'bold',size = 12),
  16. )

绘图技巧 | 绘图显著标明技巧 - 图2
Example of ggforce::geom_mark_circle()

geom_mark_ellipse()

  1. ggplot(iris, aes(Petal.Length, Petal.Width)) +
  2. geom_mark_ellipse(aes(fill = Species, label = Species),
  3. con.cap = 0,label.fill='gray',
  4. label.colour="black") +
  5. geom_point(shape=21,aes(fill=Species),colour="black",size=3) +
  6. scale_fill_nejm() +
  7. labs(
  8. title = "Example of <span style='color:#D20F26'>ggforce::geom_mark_ellipse()</span>",
  9. subtitle = "processed charts with <span style='color:#1A73E8'>geom_mark_ellipse()</span>",
  10. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  11. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  12. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  13. size = 20, margin = margin(t = 1, b = 12)),
  14. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  15. plot.caption = element_markdown(face = 'bold',size = 12),
  16. )

绘图技巧 | 绘图显著标明技巧 - 图3
Example of ggforce::geom_mark_ellipse()

geom_mark_hull()

  1. ggplot(iris, aes(Petal.Length, Petal.Width)) +
  2. geom_mark_hull(aes(fill = Species, label = Species),
  3. con.cap = 0,label.fill='gray',
  4. label.colour="black") +
  5. geom_point(shape=21,aes(fill=Species),colour="black",size=3) +
  6. scale_fill_nejm() +
  7. labs(
  8. title = "Example of <span style='color:#D20F26'>ggforce::geom_mark_hull()</span>",
  9. subtitle = "processed charts with <span style='color:#1A73E8'>geom_mark_hull()</span>",
  10. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  11. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  12. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  13. size = 20, margin = margin(t = 1, b = 12)),
  14. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  15. plot.caption = element_markdown(face = 'bold',size = 12),
  16. )

绘图技巧 | 绘图显著标明技巧 - 图4
Example of ggforce::geom_mark_hull()()
以上就是对在R中使用注释列举的几个小例子,当然,可能还不只这些。

Python 注释操作

介绍完R绘制注释(annotate)的方法,小编这里再简单介绍下Python的注释(annotate)方法,这里主要介绍Matplotlib的注释方法,如下:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. fig, ax = plt.subplots(figsize=(7,5),dpi=100)
  4. plt.rcParams['font.family'] = ['Times New Roman']
  5. t = np.arange(0.0, 5.0, 0.01)
  6. s = np.cos(2*np.pi*t)
  7. line, = ax.plot(t, s, lw=3,color="#BC3C28")
  8. # 各种annotate样式
  9. ax.annotate(
  10. 'straight',
  11. xy=(0, 1), xycoords='data',
  12. xytext=(-50, 30), textcoords='offset points',
  13. arrowprops=dict(arrowstyle="->"))
  14. ax.annotate(
  15. 'arc3,\nrad 0.2',
  16. xy=(0.5, -1), xycoords='data',
  17. xytext=(-80, -60), textcoords='offset points',
  18. arrowprops=dict(arrowstyle="->",
  19. connectionstyle="arc3,rad=.2"))
  20. ax.annotate(
  21. 'arc,\nangle 50',
  22. xy=(1., 1), xycoords='data',
  23. xytext=(-90, 50), textcoords='offset points',
  24. arrowprops=dict(arrowstyle="->",
  25. connectionstyle="arc,angleA=0,armA=50,rad=10"))
  26. ax.annotate(
  27. 'arc,\narms',
  28. xy=(1.5, -1), xycoords='data',
  29. xytext=(-80, -60), textcoords='offset points',
  30. arrowprops=dict(
  31. arrowstyle="->",
  32. connectionstyle="arc,angleA=0,armA=40,angleB=-90,armB=30,rad=7"))
  33. ax.annotate(
  34. 'angle,\nangle 90',
  35. xy=(2., 1), xycoords='data',
  36. xytext=(-70, 30), textcoords='offset points',
  37. arrowprops=dict(arrowstyle="->",
  38. connectionstyle="angle,angleA=0,angleB=90,rad=10"))
  39. ax.annotate(
  40. 'angle3,\nangle -90',
  41. xy=(2.5, -1), xycoords='data',
  42. xytext=(-80, -60), textcoords='offset points',
  43. arrowprops=dict(arrowstyle="->",
  44. connectionstyle="angle3,angleA=0,angleB=-90"))
  45. ax.annotate(
  46. 'angle,\nround',
  47. xy=(3., 1), xycoords='data',
  48. xytext=(-60, 30), textcoords='offset points',
  49. bbox=dict(boxstyle="round", fc="0.8"),
  50. arrowprops=dict(arrowstyle="->",
  51. connectionstyle="angle,angleA=0,angleB=90,rad=10"))
  52. ax.annotate(
  53. 'angle,\nround4',
  54. xy=(3.5, -1), xycoords='data',
  55. xytext=(-70, -80), textcoords='offset points',
  56. size=20,
  57. bbox=dict(boxstyle="round4,pad=.5", fc="0.8"),
  58. arrowprops=dict(arrowstyle="->",
  59. connectionstyle="angle,angleA=0,angleB=-90,rad=10"))
  60. ax.annotate(
  61. 'angle,\nshrink',
  62. xy=(4., 1), xycoords='data',
  63. xytext=(-60, 30), textcoords='offset points',
  64. bbox=dict(boxstyle="round", fc="0.8"),
  65. arrowprops=dict(arrowstyle="->",
  66. shrinkA=0, shrinkB=10,
  67. connectionstyle="angle,angleA=0,angleB=90,rad=10"))
  68. ax.annotate('', xy=(4., 1.), xycoords='data',
  69. xytext=(4.5, -1), textcoords='data',
  70. arrowprops=dict(arrowstyle="<->",
  71. connectionstyle="bar",
  72. ec="k",
  73. shrinkA=5, shrinkB=5))
  74. # 定制化操作
  75. ax.set(xlim=(-1, 5), ylim=(-4, 3))
  76. for spine in ['top','bottom','left','right']:
  77. ax.spines[spine].set_visible(False)
  78. ax.tick_params(left=False,labelleft=False,bottom=False,labelbottom=False)
  79. ax.set_title("Example Of Matplotlib.annotate()",size=15,fontweight="bold")

绘图技巧 | 绘图显著标明技巧 - 图5
Example Of Matplotlib.annotate()