r语言中x=coords[,1]是什么意思

Python011

r语言中x=coords[,1]是什么意思,第1张

abline 中的3,2和y=3+2*x中的3,2含义一样;

当然abline还有其他用法,abline参考帮助;

str()用来显示任意R对象,df是F分布的分布函数,也常用来表示data.frame的对象;具体含义参考语境;

R 有几种用于制作图形的系统,但 ggplot2 是最优雅和最通用的系统之一。与大多数其他图形包不同,ggplot2 具有基于图形语法的底层语法,它允许您通过组合独立组件来组合图形。如果想要更加了解ggplot2,请阅读 ggplot2: Elegant Graphics for Data Analysis ,可以从 https://ggplot2-book.org/getting-started.html 学习本书

所有的图都由数据data,想要可视化的信息,映射mapping(即数据变量如何映射到美学属性的描述)组成

1. 图层(layers) 是几何元素和统计变换的集合。几何对象,简称 geoms ,代表你在图中实际看到的东西:点、线、多边形等等。 统计转换,简称 stats ,总结数据:例如,装箱和计数观察,以创建一个直方图,或拟合一个线性模型。

2. Scales 将数据空间中的值映射到美学空间中的值。这包括颜色、形状和大小的使用。Scale还绘制图例和轴,这使得从图中读取原始数据值成为可能(反向映射)。

3. 坐标(coords)坐标系统描述如何将数据坐标映射到图形的平面。它还提供了轴和网格线来帮助读取图形。我们通常使用笛卡尔坐标系,但也可以使用其他一些坐标系,包括极坐标和地图投影。

4. 刻面(facet) 指定如何拆分数据子集并将其显示为小倍数。这也被称为条件反射或网格/格子。

5. theme 控制更精细的显示点,如字体大小和背景颜色。

ggplot2有许多参数,可根据需求自行选取,具体参数详情可见 https://ggplot2.tidyverse.org/reference/index.html

基础绘图:由 ggplot(data,aes(x,y))+geom_ 开始,至少包含这三个组件,可以通过"+"不断的添加layers, scales, coords和facets。

Geoms :几何对象,通常,您将使用geom_函数创建层,以下为常用的图形:

geom_bar() :直方图,条形图

geom_boxplot() :box图

geom_density() :平滑密度估计曲线

geom_dotplot() :点图

geom_point() :点图

geom_violin() :小提琴图

aes(),颜色、大小、形状和其他审美属性

要向绘图添加其他变量,我们可以使用其他美学,如颜色、形状和大小。

按照属性定义

它们的工作方式与 x 和 y 相同,aes():

aes(displ, hwy, colour = class) #按照某个属性着色

aes(displ, hwy, shape = drv) #按照某个属性定义

aes(displ, hwy, size = cyl) #按照某个属性定义

整体自定义

geom_xxx(colour =自定义颜色)

geom_xxx(shape=形状编号)

geom_xxx(size =编号大小定义 0-10)

注意根据需求按照aes()还是geom进行添加属性

以下为R语言中各shape形状编号

scale控制如何将数据值转换为视觉属性的细节。

labs()和lims() 是对标签和限制进行最常见调整。

labs() ,主要对图形进行调整,注释等

labs()括号内参数:title主标题,subtitle副标题,caption右下角描述,tag左上角

xlab() ,x轴命名

ylab() ,y轴命名

ggtitle() ,标题

lims()

xlim() , xlim(a,b) 限制坐标(a,b)

ylim() , ylim(a,b) 限制坐标(a,b)

scale_alpha() 透明度尺度

scale_shape() , 搭配aes(shape=某个属性)使用

参数:name ,solid =T/F是否填充

scale_size()搭配aes(size=某个属性)使用

参数:name,range =c(0, 10)

1.适用于发散和定性的数据

a. scale_colour_brewer() ,scale_colour_brewer(palette =" "),scale_colour_brewer(palette ="Green ")

palette来自RcolorBrewer包,所有面板:

b. scale_colour_manual()

scale_colour_manual(values=c( )) 可以 自定义颜色 ,常用的参数

values可直接定义颜色,但是建议使用命名向量,例如

values=c("8" = "red", "4" = "blue", "6" = "darkgreen", "10" = "orange")

PS:注意在aes(colour=factor()),一定要把因素转换为factor型,否则无效

2.适用于连续的值,渐变颜色

a. scale_colour_gradient()

scale_colour_gradient (low =" ",high=" "),根据值大小定义颜色,创建两个颜色梯度(低-高),

b. scale_colour_gradient2()

scale_colour_gradient2(low = " ",mid = " ",high = " ")创建一个发散的颜色梯度(低-中-高)

c. scale_colour_gradientn()

创建一个n色渐变,scale_colour_gradientn(colours =许多R语言中的颜色面板),

默认坐标系是笛卡尔 coord_cartesian()

一般不会修改

facet_grid() ,在网格中布置面板

facet_grid(rows = vars() ) cols或rows = vars(因素),图形按列或行分割

facet_wrap()

facet_wrap(vars( ), ncol =n) , ncol或者nrow,分为多少行多少列

theme_bw() ,可以覆盖所有主题,背景变为白色,我们在文章中所用的图片大都需要该背景。

或者用 theme_classic() ,同时去除了网格线

theme() ,修改主题的组件,里面涉及多个参数,根据需求调整

常见参数:

legend.position,图例的位置,包括 "left" 左, "right" 右, "bottom" 下, "top" 上和"none",不显示

可以使用数据标号“text()”函数text()函数跟在画图函数语句后面,即先画出图,再标号。

下面为来自R的text()函数使用方法(疑难词汇已经标出)

Description

text draws the strings given in the vector(矢量) labels at the coordinates(坐标) given by x and y. y may be missing since xy.coords(x, y) is used for construction of the coordinates.

Usage

text(x, ...)

## Default S3 method:

text(x, y = NULL, labels = seq_along(x$x), adj = NULL,pos = NULL, offset = 0.5, vfont =NULL,cex = 1, col = NULL, font = NULL, ...)

Arguments

x, y

numeric(数) vectors(矢量) of coordinates(坐标) where the text labels should be written. If the length of x and y differs, the shorter one is recycled.

labels

a character vector or expression specifying the text to be written. An attempt is made to coerce(强制) other language objects (names and calls) to expressions, and vectors and other classed objects to character vectors byas.character. If labels is longer than x and y, the coordinates(坐标) are recycled to the length of labels.

adj

one or two values in [0, 1] which specify(指定) the x (and optionally(可选择的) y) adjustment(调整) of the labels(标签). On most devices(装置) values outside that interval will also work.

pos

a position specifier for the text. If specified this overrides(代理佣金) any adj value given. Values of 1, 2, 3 and 4, respectively(分别地) indicate(表明) positions below, to the left of, above and to the right of the specified coordinates.

offset

when pos is specified(指定), this value gives the offset(抵消) of the label(标签) from the specified coordinate(坐标) in fractions(分数) of a character width.

vfont

NULL for the current font family, or a character vector(矢量) of length 2 for Hershey vector fonts. The first element(元素) of the vector selects a typeface and the second element selects a style. Ignored(驳回诉讼) if labels is an expression.

cex

numeric character expansion factor(因素)multiplied by par("cex") yields(产量) the final character size. NULL and NA are equivalent to 1.0.

col, font

the color and (if vfont = NULL) font to be used, possibly vectors(矢量). These default to the values of the global graphical parameters in par().

...

further graphical parameters (from par), such as srt, family and xpd.

Details

labels must be of type character or expression (or be coercible(可强迫的) to such a type). In the latter case, quite a bit of mathematical(数学的) notation(符号) is available such as sub- and superscripts(上标), greek letters,fractions(分数), etc.

adj allows adjustment of the text with respect to (x, y). Values of 0, 0.5, and 1 specify(指定) left/bottom, middle and right/top alignment(队列), respectively(分别地). The default is for centered text, i.e., adj = c(0.5, NA).Accurate(精确的) vertical(垂直的) centering needs character metric(度量标准) information on individual(个人的) characters which is only available on some devices(装置). Vertical alignment is done slightly differently for character strings and for expressions: adj = c(0,0) means to left-justify and to align(结盟) on the baseline for strings but on the bottom of the bounding box for expressions. This also affects vertical(垂直的) centering: for strings the centeringexcludes(排除) any descenders(下降) whereas(然而) for expressions it includes them. Using NA for strings centers them, including descenders.

The pos and offset arguments can be used in conjunction(结合) with values returned by identify to recreate(再创造) an interactively(交互式地) labelled(贴上标签的) plot(情节).

Text can be rotated(旋转的) by using graphical parameters srt (see par)this rotates about the centre set by adj.

Graphical parameters col, cex and font can be vectors(矢量) and will then be applied cyclically(周期的) to the labels (and extra values will be ignored(驳回诉讼)). NA values of font are replaced by par("font"), and similarly for col.

Labels whose x, y or labels value is NA are omitted(省略) from the plot(情节).

What happens when font = 5 (the symbol(象征) font) is selected can be both device- and locale-dependent. Most often labels will be interpreted(说明) in the Adobe symbol encoding, so e.g. "d" is delta, and "\300" is aleph.

Euro symbol

The Euro symbol may not be available in older fonts. In current versions of Adobe symbol fonts it is character 160, so text(x, y, "\xA0", font = 5) may work. People using Western European locales(场所) on Unix-alikes can probably select ISO-8895-15 (Latin-9) which has the Euro as character 165: this can also be used for postscript and pdf. It is \u20ac in Unicode, which can be used in UTF-8 locales(场所).

In all the European Windows encodings the Euro is symbol(象征) 128 and \u20ac will work in all locales: however not all fonts will include it. It is not in the symbol font used for windows and related devices(装置), including the Windows printer.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth &Brooks/Cole.

Murrell, P. (2005) R Graphics. Chapman(叫卖小贩) &Hall/CRC Press.

See Also

text.formula for the formula(公式) methodmtext, title, Hershey for details on Hershey vector(矢量) fonts, plotmath for details and more examples on mathematical(数学的) annotation(注释).

Examples

plot(-1:1, -1:1, type = "n", xlab = "Re", ylab = "Im")

K <- 16text(exp(1i * 2 * pi * (1:K) / K), col = 2)

## The following two examples use latin1 characters: these may not

## appear correctly (or be omitted entirely).

plot(1:10, 1:10, main = "text(...) examples\n~~~~~~~~~~~~~~",

sub = "R is GNU ©, but not ® ...")

mtext("«Latin-1 accented chars»: éè øØ å<Å æ<Æ", side = 3)

points(c(6,2), c(2,1), pch = 3, cex = 4, col = "red")

text(6, 2, "the text is CENTERED around (x,y) = (6,2) by default",

cex = .8)

text(2, 1, "or Left/Bottom - JUSTIFIED at (2,1) by 'adj = c(0,0)'",

adj = c(0,0))

text(4, 9, expression(hat(beta) == (X^t * X)^{-1} * X^t * y))

text(4, 8.4, "expression(hat(beta) == (X^t * X)^{-1} * X^t * y)",

cex = .75)

text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n)))

## Two more latin1 examples

text(5, 10.2,

"Le français, c'est façile: Règles, Liberté, Egalité, Fraternité...")

text(5, 9.8,

"Jetz no chli züritüütsch: (noch ein bißchen Zürcher deutsch)")