R可视化和ggplot2

《R数据可视化手册》学习笔记3---条形图(8)标签

2023-09-17  本文已影响0人  RSP小白之路

写在前面。

条形图一般用来展示不同分类下(x轴)某个数值型变量的取值(y轴)。注意,条形的高度,有时是变量的频数,有时是变量的取值本身,需要注意区分。

条形图

我没有按照书中的章节顺序,而是根据条形高度映射数据类型图形位置图形元素进行了分类整合,使脉络更清晰,知识点更集中

同时随着ggplot2包的更新,书中的一些用法也已经不适用了,因此会做一些更正。

所使用的一些示例数据需要安装加载包gcookbook,同时也需要加载ggplot2

if(!require(gcookbook) ) install.packages("gcookbook")
library(gcookbook)
library(ggplot2)

另外,ggplot2绘图的常用基本语句需要知道:

ggplot(data = , aes(x= , y = ) ) + geom_xxxx() + ...

3. 图形元素

变量到图形的映射图形的位置调整好之后,为了让图形更加美观和个性化,还有一些图形元素可以调节。

在条形图部分,我们要学习的包括,着色条状的宽度间距标签

接下来分别学习。

3.3 标签

如何给条形图添加数据标签?

示例数据是cabbage_exp数据集:

> cabbage_exp
  Cultivar Date Weight        sd  n         se
1      c39  d16   3.18 0.9566144 10 0.30250803
2      c39  d20   2.80 0.2788867 10 0.08819171
3      c39  d21   2.74 0.9834181 10 0.31098410
4      c52  d16   2.26 0.4452215 10 0.14079141
5      c52  d20   3.11 0.7908505 10 0.25008887
6      c52  d21   1.47 0.2110819 10 0.06674995

标签在条形顶端下方,注意vjust的变化:

ggplot(data = cabbage_exp, aes(x= interaction(Date,Cultivar  ) ,y = Weight  ) )  + 
  geom_bar( stat = "identity" ) +
  geom_text(aes(label = Weight), vjust = 1.5, colour = 'white')

[图片上传失败...(image-6f6bc6-1694997688449)]

标签在条形顶端上方,注意vjust的变化:

ggplot(data = cabbage_exp, aes(x= interaction(Date,Cultivar  ) ,y = Weight  ) )  + 
  geom_bar( stat = "identity" ) +
  geom_text(aes(label = Weight), vjust = -1, colour = 'black')

[图片上传失败...(image-6b160a-1694997688449)]

ggplot(data = cabbage_exp, aes(x= Date  ,y = Weight  ,fill = Cultivar ) )  + 
  geom_bar( stat = "identity" , position = 'dodge') +
  geom_text(aes(label = Weight), vjust = -1, colour = 'black')

否则会出现错位:

[图片上传失败...(image-917d4-1694997688449)]

position_dodge在geom_bargeom_text中设置一致,分类间距默认0.9

ggplot(data = cabbage_exp, aes(x= Date  ,y = Weight  ,fill = Cultivar ) )  + 
  geom_bar( stat = "identity" , position = 'dodge') +
  geom_text(aes(label = Weight), position = position_dodge(0.9),vjust = -1, colour = 'black')

[图片上传失败...(image-a9ccb9-1694997688449)]

使用plyr包对数据进行预处理,排序并进行累计求和

library(plyr)
ce <- arrange(cabbage_exp, Date,Cultivar)
ce <- ddply(ce, "Date", transform, label_y = cumsum(Weight))

ggplot(data = ce, aes(x= Date  ,y = Weight  ,fill = Cultivar ) )  + 
  geom_bar( stat = "identity" ) +
  geom_text(aes( y =label_y ,label = Weight), vjust = 1.5,colour = '#FFFFFF')

[图片上传失败...(image-b63a94-1694997688449)]

可以看到,标签的位置并不合理,我们可以对label_y的值进行适当修正,使标签居于每个色块的中央:

ce <- ddply(ce, "Date", transform, label_y = cumsum(Weight)- Weight*0.5)

ggplot(data = ce, aes(x= Date  ,y = Weight  ,fill = Cultivar ) )  + 
  geom_bar( stat = "identity" ) +
  geom_text(aes( y =label_y ,label = Weight), vjust = 1.5,colour = '#FFFFFF')

[图片上传失败...(image-7ee414-1694997688449)]

进行适当的美化,给条形图加个边框,给标签调整一下字号并加个单位:

ggplot(data = ce, aes(x= Date  ,y = Weight  ,fill = Cultivar ) )  + 
  geom_bar( stat = "identity" , width = 0.5, colour = "#000000") +
  geom_text(aes( y =label_y ,label = paste(format(Weight, nsmall =2), 'kg')), vjust = 1, size =3)

[图片上传失败...(image-8a251e-1694997688449)]


以上。

上一篇 下一篇

猜你喜欢

热点阅读