在 R 中仅绘制文本
在本文中,我们将讨论如何在 R 编程语言中仅绘制文本。
方法 1:使用par()函数和 mar 参数
在这种仅绘制文本的方法中,用户需要使用 mar 参数调用内置函数par函数来简单地绘制 R 编程语言中的空图,然后调用 text()函数将文本写入空在 R 语言中由 par函数使用 mar 参数创建的图。
Syntax: par(mar, mgp, las)
Parameters:
- mar – A numeric vector of length 4, which sets the margin sizes in the following order: bottom, left, top, and right.
- mgp – A numeric vector of length 3, which sets the axis label locations relative to the edge of the inner plot window.
- las – A numeric value indicating the orientation of the tick mark labels and any other text added to a plot after its initialization.
例子:
R
par(mar = c(0, 0, 0, 0))
plot(x = 0:10, y = 0:10, ann = F,bty = "n",type = "n",
xaxt = "n", yaxt = "n")
text(x = 5,y = 5,"Knowledge is power!\nCome with us and
contribute your knowledge about Computer Science \nto our world
of Geeks!\n~GeeksForGeeks")
R
library("ggplot2")
ggplot() +
annotate("text", x = 10, y = 10,
size = 6,
label = "Knowledge is power!\nCome with us and
contribute your knowledge about Computer Science \nto our
world of Geeks!\n~GeeksForGeeks") + theme_void()
输出:
方法二:使用ggplot2的annotate()和theme_void()函数
在这种只绘制文本的方法中,用户首先需要在 R 控制台中安装并导入,并使用 R 编程语言中的所需参数调用 ggplot2 包的 annotate() 和 theme_void() 函数,以在绘图中添加唯一文本在 R 编程语言中。
annotate()函数将几何图形添加到绘图中,但与典型的几何图形函数,几何图形的属性不是从数据框的变量映射的,而是作为向量传入的。
Syntax: annotate(geom,x = NULL,y = NULL,xmin = NULL,xmax = NULL,ymin = NULL,ymax = NULL,xend = NULL, yend = NULL,…,na.rm = FALSE)
Parameter:
- geom: name of geom to use for annotation
- x, y, xmin, ymin, xmax, ymax, xend, yend: positioning aesthetics – you must specify at least one of these.
- …: Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = “red” or size = 3.
- na.rm: If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.
theme_void()函数是一个完全空的主题,对于具有非标准坐标的绘图或绘图很有用。
例子:
电阻
library("ggplot2")
ggplot() +
annotate("text", x = 10, y = 10,
size = 6,
label = "Knowledge is power!\nCome with us and
contribute your knowledge about Computer Science \nto our
world of Geeks!\n~GeeksForGeeks") + theme_void()
输出: