2021-01-09

基础箱线图练习

h <- c(0.33, 0.48, 0.51, 0.43, 0.54, 0.44, 0.41, 0.37)
boxplot(h)

结果如下:

image.png

数据准备

#采用ToothGrowth数据集
ToothGrowth
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
> ToothGrowth
    len supp dose
1   4.2   VC  0.5
2  11.5   VC  0.5
3   7.3   VC  0.5
4   5.8   VC  0.5
5   6.4   VC  0.5
6  10.0   VC  0.5
7  11.2   VC  0.5
8  11.2   VC  0.5
9   5.2   VC  0.5
10  7.0   VC  0.5
11 16.5   VC  1.0
12 16.5   VC  1.0
13 15.2   VC  1.0
14 17.3   VC  1.0
15 22.5   VC  1.0
16 17.3   VC  1.0
17 13.6   VC  1.0
18 14.5   VC  1.0
19 18.8   VC  1.0
20 15.5   VC  1.0
21 23.6   VC  2.0
22 18.5   VC  2.0
23 33.9   VC  2.0
24 25.5   VC  2.0
25 26.4   VC  2.0
26 32.5   VC  2.0
27 26.7   VC  2.0
28 21.5   VC  2.0
29 23.3   VC  2.0
30 29.5   VC  2.0
31 15.2   OJ  0.5
32 21.5   OJ  0.5
33 17.6   OJ  0.5
34  9.7   OJ  0.5
35 14.5   OJ  0.5
36 10.0   OJ  0.5
37  8.2   OJ  0.5
38  9.4   OJ  0.5
39 16.5   OJ  0.5
40  9.7   OJ  0.5
41 19.7   OJ  1.0
42 23.3   OJ  1.0
43 23.6   OJ  1.0
44 26.4   OJ  1.0
45 20.0   OJ  1.0
46 25.2   OJ  1.0
47 25.8   OJ  1.0
48 21.2   OJ  1.0
49 14.5   OJ  1.0
50 27.3   OJ  1.0
51 25.5   OJ  2.0
52 26.4   OJ  2.0
53 22.4   OJ  2.0
54 24.5   OJ  2.0
55 24.8   OJ  2.0
56 30.9   OJ  2.0
57 26.4   OJ  2.0
58 27.3   OJ  2.0
59 29.4   OJ  2.0
60 23.0   OJ  2.0
> ToothGrowth$dose <- as.factor(ToothGrowth$dose)
> head(ToothGrowth)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5

ggplot2函数的调用

library(ggplot2)

基础箱线图

Basic box plot

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot()
p
image.png

旋转箱线图

Rotate the box plot

p + coord_flip()
image.png

缺口箱线图制作

Notched box plot

ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot(notch=TRUE)
image.png

Change outlier, color, shape and size

改变离群值的颜色,形状,大小

p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=4)
p
image.png

添加平均值

Box plot with mean points

p + stat_summary(fun=mean, geom="point", shape=23, size=4)
image.png

选定指定组展示

p + scale_x_discrete(limits=c("0.5", "2"))
image.png

将点标注上去

Box plot with dot plot

p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
image.png

Box plot with jittered points

0.2 : degree of jitter in x direction

p + geom_jitter(shape=16, position=position_jitter(0.2))
image.png

Change box plot line colors by groups

p <-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) +
  geom_boxplot()
p
image.png

Use custom color palettes

p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
image.png

Use brewer color palettes

p+scale_color_brewer(palette="Dark2")
image.png

Use grey scale

p + scale_color_grey() + theme_classic()
image.png

Use single color

ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_boxplot(fill='#A4A4A4', color="black")+
  theme_classic()
image.png

Change box plot colors by groups

p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()
p
image.png

Use custom color palettes

p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
image.png

use brewer color palettes

p+scale_fill_brewer(palette="Dark2")
image.png

Use grey scale

p + scale_fill_grey() + theme_classic()
p + theme(legend.position="top")
p + theme(legend.position="bottom")
p + theme(legend.position="none") # Remove legend
p + scale_x_discrete(limits=c("2", "0.5", "1"))
image.png

Change box plot colors by groups

ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
  geom_boxplot()
image.png

Change the position

p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
  geom_boxplot(position=position_dodge(1))
p
image.png

你可能感兴趣的:(2021-01-09)