前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >你确定!?这些基础绘图技能你全都掌握了!?

你确定!?这些基础绘图技能你全都掌握了!?

作者头像
DataCharm
发布2021-07-29 15:43:55
6770
发布2021-07-29 15:43:55
举报

在介绍完这篇关于Python-Matplotlib基础绘图属性后(这些绘图细节(字体、线类型、标记等)让你的论文配图耳目一新),有很多小伙伴私信能不能详细介绍下关于R-ggplot2的类似介绍?那么今天的这篇推文小编就系统介绍一下,详细内容如下:

  • 线类型(Line type)
  • 点形状(Point Shape)
  • 文本(font face)属性

线类型(Line type)

R-ggplot2 绘图体系中关于线条的类型主要包括:白线(0="blank")、实线(1="solid")、短虚线(3="dashed")、点线(4="dotted")、点横线(4="dotdash")、长虚线(5="longdash")、短长虚线(6="twodash")。接下来,我们使用可视化的形式展示一下,如下:

代码语言:javascript
复制
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(ggstar)

lty <- c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash")
linetypes <- data.frame(
  y = seq_along(lty),
  lty = lty
) 
ggplot(linetypes, aes(0, y)) + 
  geom_segment(aes(xend = 5, yend = y, linetype = lty)) + 
  scale_linetype_identity() + 
  geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2,fontface="bold") +
  scale_x_continuous(NULL, breaks = NULL) + 
  scale_y_reverse(NULL, breaks = NULL) +
  labs(
    title = "Example of <span style='color:#D20F26'>ggplot2:: Line type</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_segment()</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(hjust = 1,face = 'bold',size = 12))

Example Of ggplot2 Line type

如果你觉得以上的点线之际的距离无法满足自己的绘制需求,你还可以通过如下方式进行自己定义:

代码语言:javascript
复制
lty <- c("11", "18", "1f", "81", "88", "8f", "f1", "f8", "ff")
linetypes <- data.frame(
  y = seq_along(lty),
  lty = lty
) 
plot02 <- ggplot(linetypes, aes(0, y)) + 
  geom_segment(aes(xend = 5, yend = y, linetype = lty)) + 
  scale_linetype_identity() + 
  geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2,fontface="bold") +
  scale_x_continuous(NULL, breaks = NULL) + 
  scale_y_reverse(NULL, breaks = NULL) +
  labs(
    title = "Example02 of <span style='color:#D20F26'>ggplot2:: Line type</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_segment()</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(hjust = 1,face = 'bold',size = 12)
      )

Example02 Of ggplot2 Line type

「注意」:这里的linetype分别设置成"11", "18", "1f", "81", "88", "8f", "f1", "f8", "ff"。

点形状(Point Shape)

这一部分,小编介绍两种绘制点形状的方法,当然,你可以看作是是对点形状的补充,如下:

R-ggplot2 点形状介绍

R-ggplot2则通过修改geom_point()函数中shape属性进行更改,这里有0~25数字分别代表不同形状,且21~26号的点形状有fill属性,其他数字所表示的形状则只有颜色(colour)属性。详细如下:

代码语言:javascript
复制
shapes <- data.frame(
  shape = c(0:19, 22, 21, 24, 23, 20),
  x = 0:24 %/% 5,
  y = -(0:24 %% 5)
)
plot03 <- ggplot(shapes, aes(x, y)) + 
  geom_point(aes(shape = shape), size = 5, fill = "red") +
  geom_text(aes(label = shape), hjust = 0, nudge_x = 0.15) +
  scale_shape_identity() +
  expand_limits(x = 4.1) +
  scale_x_continuous(NULL, breaks = NULL) + 
  scale_y_reverse(NULL, breaks = NULL) +
  labs(
    title = "Example02 of <span style='color:#D20F26'>ggplot2:: Point Shape</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_point()</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(hjust = 1,face = 'bold',size = 12)
      )

Example Of ggplot2 Point Shape

R-ggstar 点形状介绍

除了使用ggplot2本身的点形状数字外,小编再介绍另外一个第三方包-R-ggstar包,可通过修改其提供的geom_star()函数中的starshape参数修改形状。形状汇总如下:

代码语言:javascript
复制
p1 <- show_starshapes()
p1

Example Of ggstar starshapes

更多关于ggstar::geom_star()绘图函数可参考:R-ggstar[1]

文本属性

「fontface」:

这里我们着重介绍文本的字型(fontface),R-ggplot2的字型主要分为:常规(plain)、粗体(bold)、斜体(italic)、粗斜体(bold.italic)。展示如下:

代码语言:javascript
复制
df <- data.frame(x = seq(.5,2,.5), fontface = c("plain", "bold", "italic", "bold.italic"))
ggplot(df, aes(x, 2)) + 
  geom_text(aes(label = fontface, fontface = fontface),size=5) +
  xlim(c(0,2.5)) +
  labs(
    title = "Example of <span style='color:#D20F26'>ggplot::font face</span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_text()</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(hjust = 1,face = 'bold',size = 12)
      )

Example Of ggplot2 fontface

「Justification」:对齐(左、右、居中)

字体对齐在添加文本图层中经常遇到,ggplot2中有水平(horizontal) 和垂直(vertical) 对齐,具体表示方式如下:

  • 使用字符串:"top", "middle", "bottom", "left", "center", "right"。
  • 使用0和1之间的数字表示:top = 1, middle = 0.5, bottom = 0 left = 0, center = 0.5, right = 1

展示结果如下:

代码语言:javascript
复制
just <- expand.grid(hjust = c(0, 0.5, 1), vjust = c(0, 0.5, 1))
just$label <- paste0(just$hjust, ", ", just$vjust)

ggplot(just, aes(hjust, vjust)) +
  geom_point(colour = "#0073C2", size = 5) + 
  geom_text(aes(label = label, hjust = hjust, vjust = vjust)) +
  labs(
    title = "Example of <span style='color:#D20F26'>ggplot::font justification </span>",
    subtitle = "processed charts with <span style='color:#1A73E8'>geom_text()</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(hjust = 1,face = 'bold',size = 12)
      )

Example Of ggplot2 font justification

以上就是今天小编对于R-ggplot2绘图中基本属性(线、点、字体)的一个详细介绍,更多详细内容大家可参考:ggplot2官网[2]

总结

今天这边推文详细介绍了R-ggplot2绘图体系中基础的点、线、字型的内容,希望对刚学习ggplot2绘图的小伙伴有所帮助。

参考资料

[1]

R-ggstar 点形状介绍: https://cran.r-project.org/web/packages/ggstar/vignettes/ggstar.html。

[2]

ggplot2官网: https://ggplot2.tidyverse.org/index.html。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 线类型(Line type)
  • 点形状(Point Shape)
    • R-ggplot2 点形状介绍
      • R-ggstar 点形状介绍
      • 文本属性
      • 总结
        • 参考资料
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档