|
@@ -1,4 +1,4 @@
|
|
|
-### 几何图形
|
|
|
+### 几何图形元素及其属性
|
|
|
|
|
|
#### 几何图形的视觉属性
|
|
|
|
|
@@ -44,7 +44,120 @@ ggplot(linetypes, aes(0, y)) +
|
|
|
```
|
|
|
上面标准的5个线型对应的16进制数分别为:44, 13, 1343, 73, 2262.
|
|
|
|
|
|
+##### 尺寸(size)
|
|
|
+
|
|
|
+尺寸的单位是毫米(mm)。点的尺寸指的是点的直径,线的尺寸指的是线的宽度。(?)
|
|
|
+
|
|
|
+#### 线端(lineend)和线连接(linejoin)
|
|
|
+线端和线连接是线的属性。
|
|
|
+lineend有 “round”, “butt” (默认)”,“square”三种。
|
|
|
+
|
|
|
+```{r}
|
|
|
+df <- data.frame(x = 1:3, y = c(4, 1, 9))
|
|
|
+base <- ggplot(df, aes(x, y)) + xlim(0.5, 3.5) + ylim(0, 10)
|
|
|
+base +
|
|
|
+ geom_path(size = 10) +
|
|
|
+ geom_path(size = 1, colour = "red")
|
|
|
+
|
|
|
+base +
|
|
|
+ geom_path(size = 10, lineend = "round") +
|
|
|
+ geom_path(size = 1, colour = "red")
|
|
|
+
|
|
|
+base +
|
|
|
+ geom_path(size = 10, lineend = "square") +
|
|
|
+ geom_path(size = 1, colour = "red")
|
|
|
+```
|
|
|
+线连接有:“round” (the default), “mitre”, or “bevel”三种。
|
|
|
+
|
|
|
+```{r}
|
|
|
+df <- data.frame(x = 1:3, y = c(9, 1, 9))
|
|
|
+base <- ggplot(df, aes(x, y)) + ylim(0, 10)
|
|
|
+base +
|
|
|
+ geom_path(size = 10) +
|
|
|
+ geom_path(size = 1, colour = "red")
|
|
|
+
|
|
|
+base +
|
|
|
+ geom_path(size = 10, linejoin = "mitre") +
|
|
|
+ geom_path(size = 1, colour = "red")
|
|
|
+
|
|
|
+base +
|
|
|
+ geom_path(size = 10, linejoin = "bevel") +
|
|
|
+ geom_path(size = 1, colour = "red")
|
|
|
+```
|
|
|
+
|
|
|
+#### 点的形状(shape)
|
|
|
+
|
|
|
+* 点的形状用整数表示[0,25]
|
|
|
+```{r}
|
|
|
+shapes <- data.frame(
|
|
|
+ shape = c(0:19, 22, 21, 24, 23, 20),
|
|
|
+ x = 0:24 %/% 5,
|
|
|
+ y = -(0:24 %% 5)
|
|
|
+)
|
|
|
+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_continuous(NULL, breaks = NULL)
|
|
|
+```
|
|
|
+* 如果用字符绘图符号,直接用字符即可。
|
|
|
+* "."代表一个单像素的正方形。
|
|
|
+* NA代表空,在该位置不绘制任何符号。
|
|
|
+
|
|
|
+#### 点的stroke和填充色(fill)
|
|
|
+点可以有填充色和stroke两个属性,点的尺寸是两者的和。
|
|
|
+
|
|
|
+```{r}
|
|
|
+sizes <- expand.grid(size = (0:3) * 2, stroke = (0:3) * 2)
|
|
|
+ggplot(sizes, aes(size, stroke, size = size, stroke = stroke)) +
|
|
|
+ geom_abline(slope = -1, intercept = 6, colour = "white", size = 6) +
|
|
|
+ geom_point(shape = 21, fill = "red") +
|
|
|
+ scale_size_identity()
|
|
|
+```
|
|
|
+
|
|
|
#### ggplot图形元素
|
|
|
|
|
|
##### 点(point)
|
|
|
|
|
|
+
|
|
|
+##### 文本(text)
|
|
|
+
|
|
|
+文本用geom_text函数绘制。
|
|
|
+
|
|
|
+* 字体(font family)
|
|
|
+英文字体中,“sans” (默认), “serif”, or “mono”这三种字体据说普遍适用。
|
|
|
+aes(family="mono")
|
|
|
+
|
|
|
+```{r}
|
|
|
+df <- data.frame(x = 1, y = 3:1, family = c("sans", "serif", "mono"))
|
|
|
+ggplot(df, aes(x, y)) +
|
|
|
+ geom_text(aes(label = family, family = family))
|
|
|
+```
|
|
|
+中文字体的显示经常出问题,新版本的Rstudio可能已经解决了这个问题。但在不同的图形设备或不同的平台(windows、linux、mac)中可能还会有问题,关于如何解决中文问题,将专门介绍。
|
|
|
+
|
|
|
+* 字形(font face)
|
|
|
+这个不知道应该怎么翻译,它值得是黑体、斜体等,有时也叫字体,与上面的font family显然不同,但font family也翻译成字体,有点晕。
|
|
|
+但使用时记住这四种就行了:plain(不变)、bold(加粗)、italic(倾斜)、bold.italic(加粗带倾斜)
|
|
|
+aes(fontface="italic")
|
|
|
+
|
|
|
+```{r}
|
|
|
+df <- data.frame(x = 1:4, fontface = c("plain", "bold", "italic", "bold.italic"))
|
|
|
+ggplot(df, aes(1, x)) +
|
|
|
+ geom_text(aes(label = fontface, fontface = fontface))
|
|
|
+```
|
|
|
+* 位置(justification)
|
|
|
+位置调整用名称或数字都行。名称:“top”, “middle”, “bottom”, “left”, “center”, “right”,对应的数值是:
|
|
|
+
|
|
|
+水平:top = 1, middle = 0.5, bottom = 0
|
|
|
+垂直:left = 0, center = 0.5, right = 1
|
|
|
+
|
|
|
+```{r}
|
|
|
+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 = "grey70", size = 5) +
|
|
|
+ geom_text(aes(label = label, hjust = hjust, vjust = vjust))
|
|
|
+```
|