编程学习ggplot2学习笔记

可视化学习笔记(一):ggplot2:条形图

2018-02-03  本文已影响12人  100gle

安装、加载包

#if (!require(ggplot)) (install.packages("ggplot2"))
#if (!require(gcookbook)) (install.packages("gcookbook"))
library(ggplot2) 
library(gcookbook) #使用数据集

1、基本语法

geom_bar(data, aes=(x, y), stat= )

1.1堆积条形图/分组条形图

ggplot(diamonds, aes(x= , y= , fill= ) +
    geom_bar(stat='identity')
ggplot(diamonds, aes(x= , y= , fill= ) +
    geom_bar(stat='identity', position='dodge')

1.2频数条形图

1.3百分比堆积条形图


2、调色

2.1颜色填充

颜色的填充主要使用color和fill映射参数:

对于分类变量与连续变量,着色的效果会有所不同

2.2颜色指定

如果对于填充的颜色不满意,可以使用:

scale_fill_brewer()函数
scale_fill_manual()函数

scale_fill_brewer(..., guide=T)
scale_fill_manual(values=c(...), guide=T) #通过values来指定颜色

3、宽度调整

geom_bar(..., position=position_dodge(#)

4、标签与图例

4.1标签添加

添加标签使用geom_text()函数:

geom_text(aes(x=, y=, label= ), vjust/hjust=#)

4.2图例

对于图例进行调整可以使用guides函数:

guides(....)

更多细节可以参考??ggplot2::guides

上一篇 下一篇

猜你喜欢

热点阅读