前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >这种显著标明怎么绘制?一个技巧解决,超简单~~

这种显著标明怎么绘制?一个技巧解决,超简单~~

作者头像
DataCharm
发布2022-10-25 10:21:33
5360
发布2022-10-25 10:21:33
举报
文章被收录于专栏:数据 学术 商业 新闻

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

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

R注释操作

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

R-ggplot2 注释操作

这一部分使用ggplot2中*annotate()*函数进行说明,这里小编直接给出一个具体案例,如下:

代码语言:javascript
复制
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 <span style='color:#D20F26'>ggplot2::annotate()</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>annotate()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  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),
        )

Example of ggplot2 annotate()

当然如果想要实现这种“箭头”效果,ggplot2的geom_segment()和geom_curve()都可实现,感兴趣的小伙伴可去ggplot2官网(https://ggplot2.tidyverse.org/reference/index.html) 进行探索。下面小编将介绍一种更为方便直观且简单的方法。

R-ggforce 注释操作

R-ggforce包中有几个绘图函数可以实现较为灵活的注释效果,且语法较为简单。官网为:https://ggforce.data-imaginist.com/reference/index.html。详细如下:

  • 「geom_mark_rect()」
代码语言:javascript
复制
ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_mark_rect(aes(fill = Species, label = Species),
                 con.cap = 0,label.fill='gray',
                 label.colour="black") +
  geom_point(shape=21,aes(fill=Species),colour="black",size=3) +
  scale_fill_nejm() +
  labs(
    title = "Example of <span style='color:#D20F26'>ggforce::geom_mark_rect()</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_mark_rect()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  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),
  )

Example of ggforce::geom_mark_rect()

  • 「geom_mark_circle()」
代码语言:javascript
复制
ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_mark_circle(aes(fill = Species, label = Species),
                 con.cap = 0,label.fill='gray',
                 label.colour="black") +
  geom_point(shape=21,aes(fill=Species),colour="black",size=3) +
  scale_fill_nejm() +
  labs(
    title = "Example of <span style='color:#D20F26'>ggforce::geom_mark_circle()</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_mark_circle()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  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),
  )

Example of ggforce::geom_mark_circle()

  • 「geom_mark_ellipse()」
代码语言:javascript
复制
ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_mark_ellipse(aes(fill = Species, label = Species),
                   con.cap = 0,label.fill='gray',
                   label.colour="black") +
  geom_point(shape=21,aes(fill=Species),colour="black",size=3) +
  scale_fill_nejm() +
  labs(
    title = "Example of <span style='color:#D20F26'>ggforce::geom_mark_ellipse()</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_mark_ellipse()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  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),
  )

Example of ggforce::geom_mark_ellipse()

  • 「geom_mark_hull()」
代码语言:javascript
复制
ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_mark_hull(aes(fill = Species, label = Species),
                    con.cap = 0,label.fill='gray',
                    label.colour="black") +
  geom_point(shape=21,aes(fill=Species),colour="black",size=3) +
  scale_fill_nejm() +
  labs(
    title = "Example of <span style='color:#D20F26'>ggforce::geom_mark_hull()</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_mark_hull()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  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),
  )

Example of ggforce::geom_mark_hull()()

以上就是小编对在R中使用注释列举的几个几个小例子,当然,可能还不只这些,也希望小伙伴们可以公号后台看留言告知哈~~

Python 注释操作

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

代码语言:javascript
复制
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(7,5),dpi=100)
plt.rcParams['font.family'] = ['Times New Roman']

t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=3,color="#BC3C28")
# 各种annotate样式
ax.annotate(
    'straight',
    xy=(0, 1), xycoords='data',
    xytext=(-50, 30), textcoords='offset points',
    arrowprops=dict(arrowstyle="->"))
ax.annotate(
    'arc3,\nrad 0.2',
    xy=(0.5, -1), xycoords='data',
    xytext=(-80, -60), textcoords='offset points',
    arrowprops=dict(arrowstyle="->",
                    connectionstyle="arc3,rad=.2"))
ax.annotate(
    'arc,\nangle 50',
    xy=(1., 1), xycoords='data',
    xytext=(-90, 50), textcoords='offset points',
    arrowprops=dict(arrowstyle="->",
                    connectionstyle="arc,angleA=0,armA=50,rad=10"))
ax.annotate(
    'arc,\narms',
    xy=(1.5, -1), xycoords='data',
    xytext=(-80, -60), textcoords='offset points',
    arrowprops=dict(
        arrowstyle="->",
        connectionstyle="arc,angleA=0,armA=40,angleB=-90,armB=30,rad=7"))
ax.annotate(
    'angle,\nangle 90',
    xy=(2., 1), xycoords='data',
    xytext=(-70, 30), textcoords='offset points',
    arrowprops=dict(arrowstyle="->",
                    connectionstyle="angle,angleA=0,angleB=90,rad=10"))
ax.annotate(
    'angle3,\nangle -90',
    xy=(2.5, -1), xycoords='data',
    xytext=(-80, -60), textcoords='offset points',
    arrowprops=dict(arrowstyle="->",
                    connectionstyle="angle3,angleA=0,angleB=-90"))
ax.annotate(
    'angle,\nround',
    xy=(3., 1), xycoords='data',
    xytext=(-60, 30), textcoords='offset points',
    bbox=dict(boxstyle="round", fc="0.8"),
    arrowprops=dict(arrowstyle="->",
                    connectionstyle="angle,angleA=0,angleB=90,rad=10"))
ax.annotate(
    'angle,\nround4',
    xy=(3.5, -1), xycoords='data',
    xytext=(-70, -80), textcoords='offset points',
    size=20,
    bbox=dict(boxstyle="round4,pad=.5", fc="0.8"),
    arrowprops=dict(arrowstyle="->",
                    connectionstyle="angle,angleA=0,angleB=-90,rad=10"))
ax.annotate(
    'angle,\nshrink',
    xy=(4., 1), xycoords='data',
    xytext=(-60, 30), textcoords='offset points',
    bbox=dict(boxstyle="round", fc="0.8"),
    arrowprops=dict(arrowstyle="->",
                    shrinkA=0, shrinkB=10,
                    connectionstyle="angle,angleA=0,angleB=90,rad=10"))

ax.annotate('', xy=(4., 1.), xycoords='data',
            xytext=(4.5, -1), textcoords='data',
            arrowprops=dict(arrowstyle="<->",
                            connectionstyle="bar",
                            ec="k",
                            shrinkA=5, shrinkB=5))
# 定制化操作
ax.set(xlim=(-1, 5), ylim=(-4, 3))
for spine in ['top','bottom','left','right']:
    ax.spines[spine].set_visible(False)
ax.tick_params(left=False,labelleft=False,bottom=False,labelbottom=False)
ax.set_title("Example Of Matplotlib.annotate()",size=15,fontweight="bold")

Example Of Matplotlib.annotate()

更多内容,大家可参看matplotlib官网样例即可。

总结

本期这篇推文,小编给大家简单汇总了绘图过程中一些注释(annotate) 的方法。从中可以看出,R还是比较方便的,且更加美观,还是那句话,适合自己的才是最好的,希望这篇文章能够帮助到大家。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-08-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R注释操作
    • R-ggplot2 注释操作
      • R-ggforce 注释操作
      • Python 注释操作
      • 总结
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档