|
@@ -27,10 +27,28 @@ theme_dark(base_size = 12, base_family = "")
|
|
theme_void(base_size = 12, base_family = "")
|
|
theme_void(base_size = 12, base_family = "")
|
|
|
|
|
|
参数
|
|
参数
|
|
-base_size:基本文字大小,标题字体、标签字体、坐标轴字体等如果没有设定,则用基本文字大小。
|
|
|
|
-base_family:基本字体。
|
|
|
|
-...: dotdotdot参数:
|
|
|
|
|
|
+base_size:基本文字大小,标题字体、标签字体、坐标轴字体等如果没有设定,则用基本文字大小。
|
|
|
|
+base_family:基本字体。
|
|
|
|
+...: dotdotdot参数:
|
|
|
|
+element_line,element_rect,element_text和element_blank函数设置,使用方法参考这几个函数的参数说明即可。
|
|
|
|
+text, line, rect和title是最顶层的元素,类似于抽象类,无法直接设定,可通过其子函数设置。
|
|
|
|
|
|
|
|
+```{r}
|
|
|
|
+x <- LETTERS[1:10]
|
|
|
|
+y <- abs(rnorm(10))
|
|
|
|
+thm<- theme(
|
|
|
|
+ text = element_text(color = "red", size = 16),
|
|
|
|
+ line = element_line(color = "blue"),
|
|
|
|
+ rect = element_rect(fill = "white"))
|
|
|
|
+p <- qplot(x = x, y = y, color = x, fill = x, geom = c("line", "point"), group = 1)
|
|
|
|
+ + labs(title = "The figure title.", xlab = "Factor", ylab = "Value")
|
|
|
|
+ + thm
|
|
|
|
+
|
|
|
|
+p + theme(
|
|
|
|
+ panel.background = element_rect(fill = "transparent", color = "gray"),
|
|
|
|
+ legend.key = element_rect(fill = "transparent", color = "transparent"),
|
|
|
|
+ axis.text = element_text(color = "red"))
|
|
|
|
+```
|
|
|
|
|
|
|
|
|
|
| 参数 | 内容 | 继承自 |
|
|
| 参数 | 内容 | 继承自 |
|
|
@@ -86,6 +104,286 @@ base_family:基本字体。
|
|
| strip.text.x
|
|
| strip.text.x
|
|
| strip.text.y
|
|
| strip.text.y
|
|
|
|
|
|
|
|
+#### ggplot2图形元素属性设置
|
|
|
|
+theme_bw()函数输出可以看出主题元素及其参数和默认设置:
|
|
|
|
+##### 基本对象
|
|
|
|
+* line
|
|
|
|
+ ..$ colour : chr "black"
|
|
|
|
+ ..$ size : num 0.5
|
|
|
|
+ ..$ linetype: num 1
|
|
|
|
+ ..$ lineend : chr "butt"
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_line" "element"
|
|
|
|
+* rect :List of 4
|
|
|
|
+ ..$ fill : chr "white"
|
|
|
|
+ ..$ colour : chr "black"
|
|
|
|
+ ..$ size : num 0.5
|
|
|
|
+ ..$ linetype: num 1
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_rect" "element"
|
|
|
|
+* text :List of 10
|
|
|
|
+ ..$ family : chr ""
|
|
|
|
+ ..$ face : chr "plain"
|
|
|
|
+ ..$ colour : chr "black"
|
|
|
|
+ ..$ size : num 12
|
|
|
|
+ ..$ hjust : num 0.5
|
|
|
|
+ ..$ vjust : num 0.5
|
|
|
|
+ ..$ angle : num 0
|
|
|
|
+ ..$ lineheight: num 0.9
|
|
|
|
+ ..$ margin :Classes 'margin', 'unit' atomic [1:4] 0 0 0 0
|
|
|
|
+ .. .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ ..$ debug : logi FALSE
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+* axis.line :List of 4
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..$ lineend : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_line" "element"
|
|
|
|
+* axis.line.x : list()
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_blank" "element"
|
|
|
|
+* axis.line.y : list()
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_blank" "element"
|
|
|
|
+* axis.text :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size :Class 'rel' num 0.8
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin : NULL
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* axis.text.x :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : num 1
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin :Classes 'margin', 'unit' atomic [1:4] 2.4 0 0 0
|
|
|
|
+ .. .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* axis.text.y :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ hjust : num 1
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin :Classes 'margin', 'unit' atomic [1:4] 0 2.4 0 0
|
|
|
|
+ .. .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* axis.ticks :List of 4
|
|
|
|
+ ..$ colour : chr "black"
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..$ lineend : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_line" "element"
|
|
|
|
+* axis.ticks.length :Class 'unit' atomic [1:1] 3
|
|
|
|
+ .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+* axis.title.x :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin :Classes 'margin', 'unit' atomic [1:4] 4.8 0 2.4 0
|
|
|
|
+ .. .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* axis.title.y :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : num 90
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin :Classes 'margin', 'unit' atomic [1:4] 0 4.8 0 2.4
|
|
|
|
+ .. .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* legend.background :List of 4
|
|
|
|
+ ..$ fill : NULL
|
|
|
|
+ ..$ colour : logi NA
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_rect" "element"
|
|
|
|
+* legend.margin :Class 'unit' atomic [1:1] 0.2
|
|
|
|
+ .. ..- attr(*, "unit")= chr "cm"
|
|
|
|
+ .. ..- attr(*, "valid.unit")= int 1
|
|
|
|
+* legend.key :List of 4
|
|
|
|
+ ..$ fill : NULL
|
|
|
|
+ ..$ colour : chr "grey80"
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_rect" "element"
|
|
|
|
+* legend.key.size :Class 'unit' atomic [1:1] 1.2
|
|
|
|
+ .. ..- attr(*, "unit")= chr "lines"
|
|
|
|
+ .. ..- attr(*, "valid.unit")= int 3
|
|
|
|
+* legend.key.height : NULL
|
|
|
|
+* legend.key.width : NULL
|
|
|
|
+* legend.text :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size :Class 'rel' num 0.8
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin : NULL
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+ $ legend.text.align : NULL
|
|
|
|
+ $ legend.title :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ hjust : num 0
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin : NULL
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+ $ legend.title.align : NULL
|
|
|
|
+ $ legend.position : chr "right"
|
|
|
|
+ $ legend.direction : NULL
|
|
|
|
+ $ legend.justification : chr "center"
|
|
|
|
+ $ legend.box : NULL
|
|
|
|
+
|
|
|
|
+* panel.background :List of 4
|
|
|
|
+ ..$ fill : chr "white"
|
|
|
|
+ ..$ colour : logi NA
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_rect" "element"
|
|
|
|
+* panel.border :List of 4
|
|
|
|
+ ..$ fill : logi NA
|
|
|
|
+ ..$ colour : chr "grey50"
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_rect" "element"
|
|
|
|
+* panel.grid.major :List of 4
|
|
|
|
+ ..$ colour : chr "grey90"
|
|
|
|
+ ..$ size : num 0.2
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..$ lineend : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_line" "element"
|
|
|
|
+* panel.grid.minor :List of 4
|
|
|
|
+ ..$ colour : chr "grey98"
|
|
|
|
+ ..$ size : num 0.5
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..$ lineend : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_line" "element"
|
|
|
|
+* panel.margin :Class 'unit' atomic [1:1] 6
|
|
|
|
+ .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ $ panel.margin.x : NULL
|
|
|
|
+ $ panel.margin.y : NULL
|
|
|
|
+ $ panel.ontop : logi FALSE
|
|
|
|
+
|
|
|
|
+* strip.background :List of 4
|
|
|
|
+ ..$ fill : chr "grey80"
|
|
|
|
+ ..$ colour : chr "grey50"
|
|
|
|
+ ..$ size : num 0.2
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_rect" "element"
|
|
|
|
+* strip.text :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : chr "grey10"
|
|
|
|
+ ..$ size :Class 'rel' num 0.8
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin : NULL
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* strip.text.x :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin :Classes 'margin', 'unit' atomic [1:4] 6 0 6 0
|
|
|
|
+ .. .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* strip.text.y :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : num -90
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin :Classes 'margin', 'unit' atomic [1:4] 0 6 0 6
|
|
|
|
+ .. .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* strip.switch.pad.grid:Class 'unit' atomic [1:1] 0.1
|
|
|
|
+ .. ..- attr(*, "unit")= chr "cm"
|
|
|
|
+ .. ..- attr(*, "valid.unit")= int 1
|
|
|
|
+* strip.switch.pad.wrap:Class 'unit' atomic [1:1] 0.1
|
|
|
|
+ .. ..- attr(*, "unit")= chr "cm"
|
|
|
|
+ .. ..- attr(*, "valid.unit")= int 1
|
|
|
|
+
|
|
|
|
+* plot.background :List of 4
|
|
|
|
+ ..$ fill : NULL
|
|
|
|
+ ..$ colour : chr "white"
|
|
|
|
+ ..$ size : NULL
|
|
|
|
+ ..$ linetype: NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_rect" "element"
|
|
|
|
+* plot.title :List of 10
|
|
|
|
+ ..$ family : NULL
|
|
|
|
+ ..$ face : NULL
|
|
|
|
+ ..$ colour : NULL
|
|
|
|
+ ..$ size :Class 'rel' num 1.2
|
|
|
|
+ ..$ hjust : NULL
|
|
|
|
+ ..$ vjust : NULL
|
|
|
|
+ ..$ angle : NULL
|
|
|
|
+ ..$ lineheight: NULL
|
|
|
|
+ ..$ margin :Classes 'margin', 'unit' atomic [1:4] 0 0 7.2 0
|
|
|
|
+ .. .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ ..$ debug : NULL
|
|
|
|
+ ..- attr(*, "class")= chr [1:2] "element_text" "element"
|
|
|
|
+* plot.margin :Classes 'margin', 'unit' atomic [1:4] 6 6 6 6
|
|
|
|
+ .. ..- attr(*, "unit")= chr "pt"
|
|
|
|
+ .. ..- attr(*, "valid.unit")= int 8
|
|
|
|
+ - attr(*, "class")= chr [1:2] "theme" "gg"
|
|
|
|
+ - attr(*, "complete")= logi TRUE
|
|
|
|
+ - attr(*, "validate")= logi TRUE
|
|
|
|
+
|
|
#### ggplot2的预设主题。
|
|
#### ggplot2的预设主题。
|
|
|
|
|
|
```{r}
|
|
```{r}
|
|
@@ -130,22 +428,9 @@ p + theme_classic()
|
|
p + theme_void()
|
|
p + theme_void()
|
|
```
|
|
```
|
|
|
|
|
|
|
|
+#### 自定义主题
|
|
|
|
|
|
-但dotdotdot(···)参数却内涵丰富,它可以设置很多内容。
|
|
|
|
-
|
|
|
|
-除一些尺寸设置有关的内容外(需要用grid包的unit函数设置),几乎所有元素都在theme函数内使用 element_line,element_rect,element_text和element_blank函数设置,使用方法参考这几个函数的参数说 明即可,这里不再一一举例说明。
|
|
|
|
-text, line, rect和title是最顶层的元素,理论上可以做全局设定,但当前版本ggplot2还没有实现,可以根据情况做一些调整:
|
|
|
|
-
|
|
|
|
-```{r}
|
|
|
|
-x <- LETTERS[1:10]
|
|
|
|
-y <- abs(rnorm(10))
|
|
|
|
-p <- qplot(x = x, y = y, color = x, fill = x, geom = c("line", "point"), group = 1) + labs(title = "The figure title.", xlab = "Factor", ylab = "Value") + theme(text = element_text(color = "red", size = 16), line = element_line(color = "blue"), rect = element_rect(fill = "white"))
|
|
|
|
-p + theme(panel.background = element_rect(fill = "transparent", color = "gray"), legend.key = element_rect(fill = "transparent", color = "transparent"), axis.text = element_text(color = "red"))
|
|
|
|
-```
|
|
|
|
-
|
|
|
|
-## 2 自定义主题
|
|
|
|
-
|
|
|
|
-图形细节设置虽然繁琐,但是在R中可以相当简单。由于自己使用的或者杂志要求的图形外观一般都很固定,我们可以使用ggplot2的theme函数非常方便地定义自己的图形主题。下面是我自用的一个主题函数,主要作的改动是坐标轴刻度朝向和统一了图形各个区域的背景颜色:
|
|
|
|
|
|
+图形细节设置虽然繁琐,但是在R中可以相当简单。由于自己使用的或者杂志要求的图形外观一般都很固定,我们可以使用ggplot2的theme函数非常方便地定义自己的图形主题。坐标轴刻度朝向和统一了图形各个区域的背景颜色:
|
|
|
|
|
|
```{r}
|
|
```{r}
|
|
##' A nice-looking ggplot2 theme: inward axis ticks, legend title excluded, and uniform background.
|
|
##' A nice-looking ggplot2 theme: inward axis ticks, legend title excluded, and uniform background.
|