R语言中绘制正负值条形图

标签:
r数据可视化 |
分类: R语言 |
> require(gcookbook)
>#导入gcookbook包
Loading required package: gcookbook
> View(climate)
#查看 climate数据文件
> data(climate)
#载入climate数据
> csub <-
subset(climate,Source=="Berkeley" & Year>=1900)
#筛选出Berkeley这个地方从1900年开始后的数据
> csub$pos <-
csub$Anomaly10y>=0
#在筛选过的数据中加入一列,用来判断是正增长还是负增长
> View(csub)
#查看一下整理过的数据集
> require(ggplot2)
#载入ggplot2包
Loading required package: ggplot2
>
ggplot(csub,aes(x=Year,y=Anomaly10y,fill=pos))+geom_bar(stat="identity")
#图表X轴以年划分,Y轴是变化值,颜色用pos(TRUE/FALSE)区分,结果见下图:
Warning message:
Stacking not well defined when ymin !=
0
>ggplot(csub,aes(x=Year,y=Anomaly10y,fill=Anomaly10y))+geom_bar(stat="identity",position="identity")
#这个命令跟上图的主要区别在于,现在条形的填充色根据Anomaly10y的实际数值来填充,所以是渐变色,见下图:
前一篇:R语言中绘制彩色相关矩阵图
后一篇:R语言中绘制ECDF概率图