# 使用ggplot2绘制条形图

1,568

```x <- c('A','B','C','D','E')
y <- c(13,22,16,31,8)
df <- data.frame(x= x, y = y)
ggplot(data = df, mapping = aes(x = x, y = y)) + geom_bar(stat= 'identity')```

```set.seed(1234)
x <- sample(c(1,2,4,6,7), size = 1000, replace = TRUE,prob = c(0.1,0.2,0.2,0.3,0.2))
ggplot(data = data.frame(x = x), mapping= aes(x = x, y = ..count..)) + geom_bar(stat = 'count')```

`ggplot(data = data.frame(x = x3), mapping = aes(x = factor(x), y = ..count..))+ geom_bar(stat = 'count')`

`ggplot(data = data.frame(x = x), mapping = aes(x = factor(x), y = ..count..))+ geom_bar(stat = 'count', fill = 'steelblue', colour = 'darkred')`

colours())]返回27种红色。

```x <- rep(1:5, each = 3)
y <- rep(c('A','B','C'),times = 5)
set.seed(1234)
z <- round(runif(min = 10, max = 20, n = 15)) df <- data.frame(x= x, y = y, z = z)
ggplot(data = df, mapping = aes(x = factor(x), y = z,fill = y)) + geom_bar(stat = 'identity', position = 'dodge')```

`ggplot(data = df, mapping = aes(x = factor(x), y = z, fill = y)) + geom_bar(stat= 'identity', position = 'stack')`

`ggplot(data = df, mapping = aes(x = factor(x), y = z, fill = y)) + geom_bar(stat= 'identity', position = 'stack') + guides(fill = guide_legend(reverse= TRUE))`

guides()函数将图例引到fill属性中，再使图例反转即可。

`ggplot(data = df, mapping = aes(x = factor(x), y = z, fill = y)) + geom_bar(stat= 'identity', position = 'fill')`

scale_fill_brewer()函数使用R自带的ColorBrewer画板

`ggplot(data = df, mapping = aes(x = factor(x), y = z, fill = y)) + geom_bar(stat= ‘identity’, position = ‘dodge’) + scale_fill_brewer(palette = ‘Accent’)`

scale_fill_manual()函数允许用户给指定的分类水平设置响应的色彩，个人觉得这个比较方便

```col <- c('darkred','skyblue','purple')
ggplot(data = df, mapping =aes(x = factor(x), y = z, fill = y)) + geom_bar(stat = 'identity', colour= 'black', position = 'dodge') + scale_fill_manual(values = col, limits= c('B','C','A')) + xlab('x')```

```#不经排序的条形图，默认按x值的顺序产生条形图 x <- c('A','B','C','D','E','F','G')
y <-c('xx','yy','yy','xx','xx','xx','yy')
z <- c(10,33,12,9,16,23,11) df
<- data.frame(x = x, y = y, z = z)
ggplot(data = df, mapping = aes(x= x, y = z, fill = y)) + geom_bar(stat = 'identity')```

`ggplot(data = df, mapping = aes(x = reorder(x, z), y = z, fill = y)) +geom_bar(stat = 'identity') + xlab('x')`

```set.seed(1234)
x = 1980 + 1:35
y = round(100*rnorm(35))
df = data.frame(x= x, y = y) #判断y是否为正值 df <- transform="" df="" judge="ifelse(y">0,'Yes', 'No'))
ggplot(data = df, mapping = aes(x = x, y = y, fill = judge))+ geom_bar(stat = 'identity', position = 'identity') + scale_fill_manual(values
= c('blue','red'), guide = FALSE) + xlab('Year')```

stat参数和position参数均设置为identity，目的是图形绘制不要求对原始数据做任何的变换，包括统计变换和图形变换，排除图例可以通过scale_fill_manual()函数将参数guide设置为FALSE，同时该函数还可以自定义填充色，一举两得。

geom_bar()函数可以非常灵活的将条形图的条形宽度进行变宽或变窄设置,具体通过函数的width参数实现，width的最大值为1，默认为0.9。

```x = c('A','B','C','D','E')
y = c(10,20,15,22,18)
df = data.frame(x = x, y = y) #不作任何条形宽度的调整 ggplot(data = df, mapping = aes(x = x, y = y)) +geom_bar(stat = 'identity', fill = 'steelblue', colour = 'black')```

`ggplot(data = df, mapping = aes(x = x, y = y)) + geom_bar(stat = ‘identity’,fill = ‘steelblue’, colour = ‘black’, width = 1)`

```x <- rep(1:5, each = 3)
y <- rep(c('A','B','C'),times = 5) set.seed(1234)
z <- round(runif(min = 10, max = 20, n = 15)) df <- data.frame(x= x, y = y, z = z)
#不作任何条形宽度和条形距离的调整
ggplot(data = df, mapping = aes(x= factor(x), y = z, fill = y)) + geom_bar(stat = 'identity', position ='dodge')```

# 调整条形宽度和条形距离

`ggplot(data = df, mapping = aes(x = factor(x), y = z, fill = y)) + geom_bar(stat= 'identity', width = 0.5, position = position_dodge(0.7))`

geom_text()函数可以方便的在图形中添加数值标签，具体微调从几个案例开始：

```x <- rep(1:5, each = 3)
y <- rep(c('A','B','C'),times = 5) set.seed(1234)
z <- round(runif(min = 10, max = 20, n = 15)) df <- data.frame(x= x, y = y, z = z)
ggplot(data = df, mapping = aes(x = interaction(x,y),
y = z, fill = y)) + geom_bar(stat = 'identity') + geom_text(mapping = aes(label= z))```

`ggplot(data = df, mapping = aes(x = interaction(x,y), y = z, fill = y))+ geom_bar(stat = 'identity') + ylim(0,max(z)+1) + geom_text(mapping =aes(label = z), size = 8, colour = 'orange', vjust = 1)`

ylim设置条形图中y轴的范围；size调整标签字体大小，默认值为5号；colour更换标签颜色；vjust调整标签位置，1为分界线，越大于1，标签越在条形图上界下方，反之则越在条形图上上界上方。

```ggplot(data = df, mapping = aes(x = x, y = z, fill = y)) + geom_bar(stat
= 'identity', position = 'dodge') + geom_text(mapping = aes(label = z),
size = 5, colour = 'black', vjust = 1, hjust = .5, position = position_dodge(0.9))```

= position_dodge(0.9)参数。

```ggplot(data = df, mapping = aes(x = x, y = z, fill = y)) + geom_bar(stat
= 'identity', position = 'stack') + geom_text(mapping = aes(label = z),
size = 5, colour = 'black', vjust = 3.5, hjust = .5, position = position_stack())```

= position_stack()参数。

R数据可视化手册

R语言_ggplot2：数据分析与图形艺术

• 版权声明 本文源自 数据人 整理
• 转载请务必保留本文链接：https://www.plob.org/article/13056.html