在介绍完这篇关于Python-Matplotlib基础绘图属性后(这些绘图细节(字体、线类型、标记等)让你的论文配图耳目一新),有很多小伙伴私信能不能详细介绍下关于R-ggplot2的类似介绍?那么今天的这篇推文小编就系统介绍一下,详细内容如下:
R-ggplot2 绘图体系中关于线条的类型主要包括:白线(0="blank")、实线(1="solid")、短虚线(3="dashed")、点线(4="dotted")、点横线(4="dotdash")、长虚线(5="longdash")、短长虚线(6="twodash")。接下来,我们使用可视化的形式展示一下,如下:
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
如果你觉得以上的点线之际的距离无法满足自己的绘制需求,你还可以通过如下方式进行自己定义:
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"。
这一部分,小编介绍两种绘制点形状的方法,当然,你可以看作是是对点形状的补充,如下:
R-ggplot2则通过修改geom_point()函数中shape属性进行更改,这里有0~25数字分别代表不同形状,且21~26号的点形状有fill属性,其他数字所表示的形状则只有颜色(colour)属性。详细如下:
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
除了使用ggplot2本身的点形状数字外,小编再介绍另外一个第三方包-R-ggstar包,可通过修改其提供的geom_star()函数中的starshape参数修改形状。形状汇总如下:
p1 <- show_starshapes()
p1
Example Of ggstar starshapes
更多关于ggstar::geom_star()绘图函数可参考:R-ggstar[1]
「fontface」:
这里我们着重介绍文本的字型(fontface),R-ggplot2的字型主要分为:常规(plain)、粗体(bold)、斜体(italic)、粗斜体(bold.italic)。展示如下:
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) 对齐,具体表示方式如下:
展示结果如下:
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。