0%

ggplot2集锦之六绘制各种图形1

本文总结一下ggplot2包绘制各种类型的图,本文包括:散点图、条形图、折线图、条带图、面积图、阶梯图、密度图、累计分布曲线。

1.散点图

1.1 散点图

绘制散点图,用geom_point()。
数据如下:

1
2
3
4
5
6
7
8
9
10
11
12
library(ggplot2)

head(mpg)
# A tibble: 6 x 11
manufacturer model displ year cyl trans drv cty hwy fl class
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
3 audi a4 2 2008 4 manual(m6) f 20 31 p compact
4 audi a4 2 2008 4 auto(av) f 21 30 p compact
5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
1
2
ggplot(mpg,aes(x = displ,y = cty))+
geom_point()

1.2 散点图加拟合曲线

1
2
3
ggplot(mpg,aes(x = displ,y = cty))+
geom_point()+
stat_smooth()

alt 图标

2.柱状图/直方图/条形图

柱状图用geom_bar、geom_histogram、geom_col。geom_bar() 默认使用的统计变换方法是 count,而 geom_col() 使用 identity 不做变换。

2.1 简单柱状图

1
2
ggplot(mpg, aes(class))+
geom_bar()
1
2
3
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
ggplot(df, aes(trt, outcome)) +
geom_col()

2.2 水平条形图

1.翻转x、y轴:

1
2
3
ggplot(mpg, aes(class))+
geom_bar()+
coord_flip()

2.调整x、y轴:

1
2
ggplot(mpg, aes(y=class))+
geom_bar()

alt 图标

2.3 柱状堆叠图

1
2
ggplot(mpg, aes(class))+
geom_bar(aes(fill = drv))

堆积条形图,并翻转堆积顺序:

1
2
ggplot(mpg, aes(y = class)) +
geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE))

2.4 百分比条形图

1
2
3
ggplot(mpg, aes(class))+
geom_bar(aes(fill = drv),
position = 'fill')

2.5 并列分组条形图

1
2
ggplot(mpg, aes(class))+
geom_bar(aes(fill = drv), position = position_dodge())

alt 图标

2.6 双向柱状图

1
2
3
4
5
6
7
8
9
10
11
df <- data.frame(x=rep(c("x1","x2","x3"),each=2),
num=rnorm(6,mean = 3),
group=rep(c("up","down"),3))
df
x num group
1 x1 1.377343 up
2 x1 3.456258 down
3 x2 3.184073 up
4 x2 3.057976 down
5 x3 3.692116 up
6 x3 1.633762 down
1
2
3
4
5
6
ggplot(df, aes(x = factor(x,levels = unique(x)),
y = ifelse(group == "Up", value, -value),
fill = group)) +
geom_bar(stat = 'identity')+
coord_flip()

alt 图标

3. 折线图

1
2
3
4
5
6
7
8
9
10
head(economics)
## # A tibble: 6 x 6
## date pce pop psavert uempmed unemploy
## <date> <dbl> <int> <dbl> <dbl> <int>
## 1 1967-07-01 507.4 198712 12.5 4.5 2944
## 2 1967-08-01 510.5 198911 12.5 4.7 2945
## 3 1967-09-01 516.3 199113 11.7 4.6 2958
## 4 1967-10-01 512.9 199311 12.5 4.9 3143
## 5 1967-11-01 518.1 199498 12.5 4.7 3066
## 6 1967-12-01 525.8 199657 12.1 4.8 3018
1
2
ggplot(data = economics, aes(x=date, y=unemploy))+
geom_line()

4. 条带图

geom_ribbon绘制条带图,

1
2
3
4
5
6
ggplot(data = economics, aes(x=date, y=unemploy))+
geom_ribbon(aes(x = date,
ymax=unemploy+1000,
ymin=unemploy-1000),
fill="grey")+
geom_line()

4. 面积图

geom_area是特殊的条带图(ymin=0)。

1
2
ggplot(data = economics, aes(x=date, y=unemploy))+
geom_area()

5. 阶梯图

1
2
3
4
set.seed(1111)
ss <- economics[sample(1:nrow(economics), 20),]
ggplot(ss, aes(x=date, y=unemploy))+
geom_step()

alt 图标

6. 密度图

1
2
3
4
5
6
7
8
df1 <- data.frame(num=rnorm(500,mean = 50),
group=rep(c("group1"),500))
df2 <- data.frame(num=rnorm(500,mean = 52),
group=rep(c("group2"),500))
df <- rbind(df1,df2)

ggplot(df,aes(num,group=group,color=group))+
geom_density()

7. 累计分布图

1
2
ggplot(df,aes(num,group=group,color=group))+
stat_ecdf()

alt 图标

8. point-errorbars

1
2
3
4
5
6
7
8
9
10
ggplot(iris,aes(x = Species, y = Sepal.Length)) +
stat_summary(
fun.y = mean, fun.ymax = function(x) {
mean(x) + 2 * sd(x)
},
fun.ymin = function(x) {
mean(x) - 2 * sd(x)
}, geom = "pointrange",
fatten = 5
)

alt 图标

参考资料:

  1. https://zhuanlan.zhihu.com/p/336912400