R 语言绘图速查手册(58图)
本文参考 《R 语言绘图速查手册 v0.1beta》
图目录
1 基本散点图;200 个正态分布的随机数
2 point 散点图;200 个正态分布的随机数
3 geom_point 散点图;200 个正态分布的随机数
4 基本折线图;10 个正态分布的随机数
5 lines() 折线图;刹车速度与滑行距离的关系
6 geom_line() 连接观测值;美国人口失业情况折线图
7 barplot() 基本条形图;统计泊松分布随机数
8 barplot() 堆栈式条形图;分年龄的人口信息被叠加在一起
9 barplot() 按分类依次排列的条形图
10 geom_bar() 基本条形图
11 geom_bar() 堆栈式条形图
12 geom_bar() 依次排列式条形图
13 geom_bar() 比列式条形图
14 polygon() 密度图
15 polygon() 面积堆积图
16 geom_area() 堆积面积图
17 密度估计图
18 两个核密度估计图
19 geom_density() 核密度估计图
20 用 Graphics 函数画频率图
21 geom_freqpoly() 频率图
22 hist() 直方图
23 geom_hist() 直方图
24 boxplot() 箱线图
25 geom_boxplot() 箱线图
26 错误的 error bar 箱线图
27 带 error bar 的箱线图
28 vioplot() 提琴图
29 geom_violin() 提琴图
30 添加箱线图信息的提琴图
31 添加均值和标准差信息的提琴图
32 dotchart() 绘制 Cleveland 点图
33 geom_dotplot() 绘制 Cleveland 点图
34 用 heatmap() 绘制热图
35 geom_tile() 绘制热图
36 pheatmap() 绘制热图
37 主成分分析图
38 基本层次聚类图
39 dendrograms() 绘制层次聚类图
40 plot.phylo() 绘制层次聚类图
41 graphics 包里如何添加图片标题
42 ggplot2 包里如何添加图片标题
43 par() 函数 mfrow 设置多个图片同个画布
44 layout() 设置多个图片同个画布
45 cowlplot::ggdraw() 设置多个图片同 个画布
46 gridExtra::grid.arrange() 设置多个图片同个画布
47 barplot() 水平显示条形图
48 coord_ ip() 水平显示直方图
49 theme_grey() 背景
50 theme_gray() 背景
51 theme_bw() 背景
52 theme_linedraw() 背景
53 theme_light() 背景
54 ggplot2 包里如何更改背景
55 theme_classic() 背景
56 theme_dark() 背景
57 theme_void() 背景
58 去掉背景仅显示坐标轴
1 基本散点图;200 个正态分布的随机数
- 散点图用于研究两组个变量(x,y)在坐标平面上的关系。
# p 是point
# rnorm(n, mean = 0, sd = 1) 生成随机正态分布的序列
plot(rnorm(200), rnorm(200), type="p")
![](https://img.haomeiwen.com/i6634703/a36e96b1ea436bc1.png)
2 point 散点图;200 个正态分布的随机数
# type = "n" 没有对角线的意思
plot(-4:4, -4:4, type = "n")
points(rnorm(200), rnorm(200), col = "red")
![](https://img.haomeiwen.com/i6634703/8d26a4a1e4b39291.png)
3 geom_point 散点图;200 个正态分布的随机数
library(ggplot2)
df <- data.frame(x=rnorm(200), y=rnorm(200))
ggplot(df, aes(x, y))+geom_point()
![](https://img.haomeiwen.com/i6634703/a04a4d3f3b2fcdc1.png)
4 基本折线图;10 个正态分布的随机数
- 折线图用于显示随某个变量变化的数据。
# l 是line的意思
plot(1:10, rnorm(10), type="l")
![](https://img.haomeiwen.com/i6634703/3d077e62fb45f863.png)
5 lines() 折线图;刹车速度与滑行距离的关系
plot(cars, main = "Stopping Distance versus Speed")
#
lines(stats::lowess(cars))
![](https://img.haomeiwen.com/i6634703/8e34f9bb4de199e4.png)
6 geom_line() 连接观测值;美国人口失业情况折线图
ggplot(economics, aes(date, unemploy)) + geom_line()
![](https://img.haomeiwen.com/i6634703/83f643d0bc094e6b.png)
- pce 个人消费支出
- pop 人口
- psavert 个人存款率
- uempmed 每周失业人口中位数
-
unemploy 失业人口
image.png
7 barplot() 基本条形图;统计泊松分布随机数
- 条形图主要描述一组样本之间某个变量的差异情况。
# 生成 100 个服从泊松分布 λ = 5 的随机数,并对随机数做列联表统计,条形图展示了列联表统计的结果。
tN <- table(Ni <- stats::rpois(100, lambda = 5))
barplot(tN, col = rainbow(20))
![](https://img.haomeiwen.com/i6634703/f9952c32e72fad09.png)
8 barplot() 堆栈式条形图;分年龄的人口信息被叠加在一起
barplot(VADeaths)
![](https://img.haomeiwen.com/i6634703/a8d889b00c7e5aae.png)
![](https://img.haomeiwen.com/i6634703/bb27641c1a5cc3ae.png)
9 barplot() 按分类依次排列的条形图
barplot(VADeaths, beside = TRUE,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths), ylim = c(0, 110))
title(main = "Death Rates in Virginia", font.main = 4)
![](https://img.haomeiwen.com/i6634703/a8757238901fcab9.png)
10 geom_bar() 基本条形图
library(ggplot2)
ggplot(mpg, aes(class))+geom_bar()
![](https://img.haomeiwen.com/i6634703/7e0ce37af03fe825.png)
11 geom_bar() 堆栈式条形图
- melt函数对宽数据进行处理,得到长数据;
- identity 不调整位置
library(ggplot2)
library(reshape)
ggplot(data=melt(VADeaths), aes(x=X2, y=value, fill=X1)) + geom_bar(stat="identity")
![](https://img.haomeiwen.com/i6634703/ec98a32498b1a5eb.png)
12 geom_bar() 依次排列式条形图
- dodge 躲闪
ggplot(data=melt(VADeaths), aes(x=X2, y=value, fill=X1)) + geom_bar(stat="identity", position="dodge")
![](https://img.haomeiwen.com/i6634703/cc8021b1c2653c96.png)
13 geom_bar() 比列式条形图
ggplot(data=melt(VADeaths), aes(x=X2, y=value, fill=X1)) + geom_bar(stat="identity", position="fill")
![](https://img.haomeiwen.com/i6634703/817a7ca79e5a63e3.png)
14 polygon() 密度图
- 面积图表示一个连续变量的变化程度,同时也展示了部分与整体之间的关系。
-
这次我们用个钻石相关的数据来做展示,这个数据集合包含了 54000 个钻石的 价格以及其他相关指标。
image.png
d <- density(diamonds[diamonds$cut=="Ideal",]$price)
plot(d,main="",xlab = "Price")
polygon(d, col="red",border = "red")
d <- density(diamonds[diamonds$cut=="Premium",]$price)
polygon(d, col="orange",border = "orange")
d <- density(diamonds[diamonds$cut=="Good",]$price)
polygon(d, col="black",border = "black")
d <- density(diamonds[diamonds$cut=="Very Good",]$price)
polygon(d, col="green",border = "green")
d <- density(diamonds[diamonds$cut=="Fair",]$price)
polygon(d, col="yellow",border ="yellow")
![](https://img.haomeiwen.com/i6634703/d9bc55e98a3df111.png)
15 polygon() 面积堆积图
stackedPlot <- function(data, time=NULL, col=1:length(data), ...){
if (is.null(time))
time <- 1:length(data[[1]]);
plot(0, 0, xlim = range(time), ylim = c(0,max(rowSums(data))), t="n", ...);
for (i in length(data):1) {
# Die Summe bis zu aktuellen Spalte
prep.data <- rowSums(data[1:i]);
# Das Polygon muss seinen ersten und letzten Punkt auf der Nulllinie haben
prep.y <- c(0, prep.data, 0)
prep.x <- c(time[1], time, time[length(time)])
polygon(prep.x, prep.y, col=col[i], border = NA);
}
}
diamonds.data <- as.data.frame.matrix(t(table(diamonds$cut,diamonds$price)))
stackedPlot(diamonds.data)
-
这张图好美
image.png
16 geom_area() 堆积面积图
ggplot(diamonds, aes(x = price, fill = cut))+ geom_area(stat = "bin")
![](https://img.haomeiwen.com/i6634703/24e8e396b785af88.png)
17 密度估计图
- 取 200 个正态分布的随机数,画其密度估计图像。
set.seed(1234)
rating <- rnorm(200)
plot(density(rating))
![](https://img.haomeiwen.com/i6634703/42a59c9512ed9d49.png)
18 两个核密度估计图
- 在一张画布上画两个密度图,直接叠加就可以。
set.seed(1234)
rating <- rnorm(200)
rating2 <- rnorm(200, mean=.8)
plot(density(rating))
lines(density(rating2),col="red")
![](https://img.haomeiwen.com/i6634703/0d230c73155d61fb.png)
19 geom_density() 核密度估计图
ggplot(diamonds, aes(depth, colour = cut)) + geom_density()
![](https://img.haomeiwen.com/i6634703/b9b0956b3c392c2e.png)
20 用 Graphics 函数画频率图
- 频率图像同密度函数图像的区别是:前者统计出现的频数,后者统计概率密度函数。从图中直观的反应就是纵坐标的单位不一样。
-
mtcats 数据是 1974 年 Motor Trend 杂志 所刊登的一组 32 不同种类的汽车耗油量和其他特征信息
image.png
myhist <- hist(mtcars$mpg,plot = FALSE)
multiplier <- myhist$counts / myhist$density
mydensity <- density(mtcars$mpg)
mydensity$y <- mydensity$y * multiplier[1]
plot(mydensity)
![](https://img.haomeiwen.com/i6634703/94d85e3ec10f3544.png)
21 geom_freqpoly() 频率图
ggplot(diamonds, aes(price, colour = cut)) + geom_freqpoly(binwidth = 500)
![](https://img.haomeiwen.com/i6634703/70ddaef0b1ef70a4.png)
22 hist() 直方图
- 直方图是一种对数据分布情况的图形表示,它的样子同条形图相似,但直方图是 用面积而并非单一的高度来表示数量(同分布相关的图,都是用面积来表示数量)。
-
我们用世界主要大陆地区的数据来做演示,islands 数据统计了主要大陆和岛屿的面积信息
image.png
hist(sqrt(islands), breaks = 12, col = "lightblue", border = "pink")
![](https://img.haomeiwen.com/i6634703/095909b039b242ca.png)
23 geom_hist() 直方图
ggplot(as.data.frame(islands), aes(sqrt(islands))) + geom_histogram()
![](https://img.haomeiwen.com/i6634703/ffc219a2c6e3dfb7.png)
24 boxplot() 箱线图
- 箱线图是利用数据中的五个统计量(从下往上依次是):最小值、第一四分位数、 中位数、第三四分位数与最大值来描述数据的一种方法,它也可以粗略地看出数据是否具有有对称性,分布的分散程度等信息,特别可以用于对几个样本的比较。
-
第一列是昆虫数量,第二列是喷 雾器种类。
image.png
boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
![](https://img.haomeiwen.com/i6634703/5cfb7047253d3295.png)
25 geom_boxplot() 箱线图
ggplot(InsectSprays, aes(spray, count))+geom_boxplot()
![](https://img.haomeiwen.com/i6634703/3ddabf72a3f49f5e.png)
26 错误的 error bar 箱线图
ggplot(InsectSprays, aes(spray, count))+ geom_boxplot()+
stat_boxplot(geom ='errorbar',width=0.5)
![](https://img.haomeiwen.com/i6634703/bdb60c9a8d8080a6.png)
27 带 error bar 的箱线图
- 正确的绘图方法时先画 error bar,再画箱线图。(注意下面代码的顺序)
ggplot(InsectSprays, aes(spray, count))+ stat_boxplot(geom ='errorbar',width=0.5)+ geom_boxplot()
![](https://img.haomeiwen.com/i6634703/ab31734b44de32d1.png)
28 vioplot() 提琴图
- 提琴图展示了数据的密度估计情况,同箱线图类似。但是箱线图只是展示了分位 数的位置,而提琴图展示了任意位置的数据密度。
install.packages("vioplot")
library(sm)
library(vioplot)
x1 <- mtcars$mpg[mtcars$cyl==4]
x2 <- mtcars$mpg[mtcars$cyl==6]
x3 <- mtcars$mpg[mtcars$cyl==8]
vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"),col="gold")
title("Violin Plots of Miles Per Gallon")
![](https://img.haomeiwen.com/i6634703/2b3159a8da0fbfc8.png)
29 geom_violin() 提琴图
ggplot(mtcars, aes(factor(cyl), mpg))+ geom_violin()
![](https://img.haomeiwen.com/i6634703/73da2108ec4acedc.png)
30 添加箱线图信息的提琴图
ggplot(mtcars, aes(factor(cyl), mpg))+ geom_violin()+ geom_boxplot(width=.1)
![](https://img.haomeiwen.com/i6634703/a3a8c1593dd02913.png)
31 添加均值和标准差信息的提琴图
ggplot(mtcars, aes(factor(cyl), mpg))+ geom_violin()+ stat_summary(fun.data = mean_sdl,
geom = "pointrange",
color = "red",
fun.args = list(mult = 1))
![](https://img.haomeiwen.com/i6634703/336c8611a086f0dd.png)
32 dotchart() 绘制 Cleveland 点图
- Cleveland 点图用于绘制有分类别的数据信息。
dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7, main="Gas Milage for Car Models",
xlab="Miles Per Gallon")
![](https://img.haomeiwen.com/i6634703/1f90af8eba68b052.png)
33 geom_dotplot() 绘制 Cleveland 点图
ggplot(mtcars,aes(x = mpg,y = row.names(mtcars), fill =row.names(mtcars))) + geom_dotplot(binaxis = "y",
stackgroups = TRUE,
binwidth = 1,
method = "histodot")
![](https://img.haomeiwen.com/i6634703/196ea4cf6bf296f4.png)
34 用 heatmap() 绘制热图
x <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)
hv <- heatmap(x,
col = cm.colors(256),
scale = "column",
RowSideColors = rc, ColSideColors = cc,
margins = c(5,10),
xlab = "specification variables", ylab = "Car Models",
main = "Heatmap of Mtcars data")
![](https://img.haomeiwen.com/i6634703/1a5c9724131ea868.png)
35 geom_tile() 绘制热图
library(reshape2)
library(ggplot2)
dat <- matrix(rnorm(100, 3, 1), ncol=10)
names(dat) <- paste("X", 1:10)
dat2 <- melt(dat, id.var = "X1")
ggplot(dat2, aes(as.factor(X1), X2, group=X2)) + geom_tile(aes(fill = value))+geom_text(aes(fill = dat2$value, label = round(dat2$value, 1))) + scale_fill_gradient(low = "white", high = "red")
![](https://img.haomeiwen.com/i6634703/30aecc27a7c5f3bf.png)
36 pheatmap() 绘制热图
library(pheatmap)
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
# 设置每一列的注释
annotation_col = data.frame(
CellType = factor(rep(c("CT1", "CT2"), 5)), Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "") # 设置每一行的注释
annotation_row = data.frame(
GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "") # 设置注释的颜色
ann_colors = list(
Time = c("white", "firebrick"),
CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
pheatmap(test,
annotation_col = annotation_col,
annotation_row = annotation_row,
annotation_colors = ann_colors)
![](https://img.haomeiwen.com/i6634703/eb65621ed5811384.png)
37 主成分分析图
- 做 PCA 时我们通常会将前两个主成分展示到坐标平面上,以此来区分样本的差异性。这种图是基本统计图形的综合展示。
-
我们用 ggbiplot 包里的 wine 数据来做主成分分析,该数据记录了意大利同一 个地区的三种葡萄酒的化学成分和其他特征。
image.png
library(devtools)
install_github("vqv/ggbiplot")
library(plyr)
library(ggbiplot)
data("wine")
wine.pca <- prcomp(wine, scale. = TRUE)
ggbiplot(wine.pca, obs.scale = 1, var.scale = 1,
groups = wine.class, ellipse = TRUE, circle = TRUE) + scale_color_discrete(name = '')
![](https://img.haomeiwen.com/i6634703/be3beabd0d2b8878.png)
38 基本层次聚类图
- 层次聚类是聚类算法的一种,通过计算样本间的相似度来构造一棵聚类树。
-
我们采用美国暴力犯罪率来展示层次聚类,USArrests 包含每个州的三种犯罪 人员被逮捕的数量以及该州城市地区人口数量。
image.png
hc <- hclust(dist(USArrests), "ave")
plot(hc)
![](https://img.haomeiwen.com/i6634703/af2186d0dc316869.png)
39 dendrograms() 绘制层次聚类图
install.packages("ggdendro")
library(ggdendro)
hc <- hclust(dist(USArrests), "ave")
hcdata <- dendro_data(hc)
ggdendrogram(hcdata, rotate=TRUE, size=2) + labs(title="Dendrogram in ggplot2")
![](https://img.haomeiwen.com/i6634703/910d9901597d86da.png)
40 plot.phylo() 绘制层次聚类图
install.packages("ape")
hc <- hclust(dist(USArrests), "ave")
library(ape)
plot(as.phylo(hc), type = "fan")
![](https://img.haomeiwen.com/i6634703/ef3002bd53d86d74.png)
41 graphics 包里如何添加图片标题
- 在 graphics 包中添加标题用 main 参数,添加子标题用 sub 参数,添加 x 轴标签用 xlab 参数,添加 y 轴标签用 ylab 参数。
plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10, main = "rpois(100, lambda = 5)",sub="this is a sub title", xlab="x axis title",ylab="y axis title"
)
![](https://img.haomeiwen.com/i6634703/38d242fcfab8031f.png)
42 ggplot2 包里如何添加图片标题
-
这里我们又用了一个新的示例数据 PlantGrowth,该数据展示了在一个试验中 控制不同的条件下植物的生长情况。
image.png
ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() +
ggtitle("Plant growth with\ndifferent treatments")+ xlab("this is xlab")+
ylab("this is ylab")
![](https://img.haomeiwen.com/i6634703/33ddd0900ccba41d.png)
43 par() 函数 mfrow 设置多个图片同个画布
attach(mtcars)
par(mfrow=c(2,2))
plot(wt,mpg, main="Scatterplot of wt vs. mpg")
plot(wt,disp, main="Scatterplot of wt vs disp")
hist(wt, main="Histogram of wt")
boxplot(wt, main="Boxplot of wt")
![](https://img.haomeiwen.com/i6634703/f9ffd93e5fdca7e6.png)
44 layout() 设置多个图片同个画布
attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE),
widths=c(3,1), heights=c(1,2))
hist(wt)
hist(mpg)
hist(disp)
![](https://img.haomeiwen.com/i6634703/696be2dd5438bfd6.png)
45 cowlplot::ggdraw() 设置多个图片同 个画布
library(cowplot)
sp <- ggplot(mtcars, aes(x = mpg, y = hp, colour = factor(cyl)))+
geom_point(size=2.5)
bp <- ggplot(diamonds, aes(clarity, fill = cut)) +
geom_bar() +
theme(axis.text.x = element_text(angle=90, vjust=0.5))
plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() + facet_grid(. ~ Species) + stat_smooth(method = "lm") + background_grid(major = 'y', minor = "none") +
panel_border()
plot_grid(sp, bp, labels=c("A", "B"), ncol = 2, nrow = 1)
ggdraw() +
draw_plot(plot.iris, 0, .5, 1, .5) +
draw_plot(sp, 0, 0, .5, .5) +
draw_plot(bp, .5, 0, .5, .5) +
draw_plot_label(c("A", "B", "C"), c(0, 0, 0.5), c(1, 0.5, 0.5), size = 15)
![](https://img.haomeiwen.com/i6634703/5116e880feff663c.png)
46 gridExtra::grid.arrange() 设置多个图片同个画布
-
在这里我们使用一个维生素 D 对豚鼠牙齿生长的影响的数据(ToothGrowth), 该数据记录了维生素 D 含量同豚鼠牙齿长度的关系。
image.png
df <- ToothGrowth
df$dose <- as.factor(df$dose)
## 计量同牙齿长度的箱线图
bp <- ggplot(df, aes(x=dose, y=len, color=dose)) + geom_boxplot() +
theme(legend.position = "none")
## 计量同牙齿长度的 Cleveland 点图
dp <- ggplot(df, aes(x=dose, y=len, fill=dose)) +
geom_dotplot(binaxis='y', stackdir='center')+ stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")+ theme(legend.position = "none")
## 计量同牙齿长度的提琴图
vp <- ggplot(df, aes(x=dose, y=len)) +
geom_violin()+
geom_boxplot(width=0.1)
## 计量同牙齿长度的散点图(jitter 抖动模式)
sc <- ggplot(df, aes(x=dose, y=len, color=dose, shape=dose)) +
geom_jitter(position=position_jitter(0.2))+ theme(legend.position = "none") + theme_gray()
library(gridExtra)
grid.arrange(bp, dp, vp, sc, ncol=2, nrow =2)
## Warning: Computation failed in `stat_summary()`:
## Hmisc package required for this function
![](https://img.haomeiwen.com/i6634703/c5497893a444adca.png)
47 barplot() 水平显示条形图
tN <- table(Ni <- stats::rpois(100, lambda = 5))
barplot(tN, col = rainbow(20), horiz=TRUE)
![](https://img.haomeiwen.com/i6634703/c4cff972bbc61b57.png)
48 coord_ ip() 水平显示直方图
ggplot(PlantGrowth, aes(x=group, y=weight))+ geom_boxplot() + coord_flip()
![](https://img.haomeiwen.com/i6634703/af61aa7d0d8771a9.png)
49 theme_grey() 背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_grey()
![](https://img.haomeiwen.com/i6634703/dfef1176d658ae37.png)
50 theme_gray() 背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_gray()
![](https://img.haomeiwen.com/i6634703/98918bced8deda65.png)
51 theme_bw() 背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_bw()
![](https://img.haomeiwen.com/i6634703/3bf1d493b6f08d5b.png)
52 theme_linedraw() 背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_linedraw()
![](https://img.haomeiwen.com/i6634703/eefd2511230a6331.png)
53 theme_light() 背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_light()
![](https://img.haomeiwen.com/i6634703/5cd8d79353d0e067.png)
54 ggplot2 包里如何更改背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_minimal()
![](https://img.haomeiwen.com/i6634703/96b14d59f03d310d.png)
55 theme_classic() 背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_classic()
![](https://img.haomeiwen.com/i6634703/a06f6fa06c1b22df.png)
56 theme_dark() 背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_dark()
![](https://img.haomeiwen.com/i6634703/ce22e1ee19388df9.png)
57 theme_void() 背景
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
theme_void()
![](https://img.haomeiwen.com/i6634703/5c2aa50771d379b4.png)
58 去掉背景仅显示坐标轴
library(ggplot2)
a <- seq(1,20)
b <- a^0.25
df <- as.data.frame(cbind(a,b))
ggplot(df, aes(a, b)) +
geom_point() +
theme(axis.line.x = element_line(color = "black"),
axis.line.y = element_line(color = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank())
![](https://img.haomeiwen.com/i6634703/150a6835d88bbf45.png)