Usage

geom_bar(mapping = NULL, data = NULL, stat = "bin", position = "stack", ...)

Arguments

mapping
The aesthetic mapping, usually constructed with aes or aes_string. Only needs to be set at the layer level if you are overriding the plot defaults.
data
A layer specific dataset - only needed if you want to override the plot defaults.
stat
The statistical transformation to use on the data for this layer.
position
The position adjustment to use for overlappling points on this layer
...
other arguments passed on to layer. This can include aesthetics whose values you want to set, not map. See layer for more details.

Description

The bar geom is used to produce 1d area plots: bar charts for categorical x, and histograms for continuous y. stat_bin explains the details of these summaries in more detail. In particular, you can use the weight aesthetic to create weighted histograms and barcharts where the height of the bar no longer represent a count of observations, but a sum over some other variable. See the examples for a practical example.

Details

The heights of the bars commonly represent one of two things: either a count of cases in each group, or the values in a column of the data frame. By default, geom_bar uses stat="bin". This makes the height of each bar equal to the number of cases in each group, and it is incompatible with mapping values to the y aesthetic. If you want the heights of the bars to represent values in the data, use stat="identity" and map a value to the y aesthetic.

By default, multiple x's occuring in the same place will be stacked a top one another by position_stack. If you want them to be dodged from side-to-side, see position_dodge. Finally, position_fill shows relative propotions at each x by stacking the bars and then stretching or squashing to the same height.

Sometimes, bar charts are used not as a distributional summary, but instead of a dotplot. Generally, it's preferable to use a dotplot (see geom_point) as it has a better data-ink ratio. However, if you do want to create this type of plot, you can set y to the value you have calculated, and use stat='identity'

A bar chart maps the height of the bar to a variable, and so the base of the bar must always been shown to produce a valid visual comparison. Naomi Robbins has a nice article on this topic. This is the reason it doesn't make sense to use a log-scaled y axis with a bar chart

Aesthetics

geom_bar understands the following aesthetics (required aesthetics are in bold):

  • x
  • alpha
  • colour
  • fill
  • linetype
  • size
  • weight

Examples

# Generate data c <- ggplot(mtcars, aes(factor(cyl))) # By default, uses stat="bin", which gives the count in each category c + geom_bar()

c + geom_bar(width=.5)

c + geom_bar() + coord_flip()

c + geom_bar(fill="white", colour="darkgreen")

# Use qplot qplot(factor(cyl), data=mtcars, geom="bar")

qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(cyl))

# When the data contains y values in a column, use stat="identity" library(plyr) # Calculate the mean mpg for each level of cyl mm <- ddply(mtcars, "cyl", summarise, mmpg = mean(mpg)) ggplot(mm, aes(x = factor(cyl), y = mmpg)) + geom_bar(stat = "identity")

# Stacked bar charts qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(vs))

qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear))

# Stacked bar charts are easy in ggplot2, but not effective visually, # particularly when there are many different things being stacked ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()

ggplot(diamonds, aes(color, fill=cut)) + geom_bar() + coord_flip()

# Faceting is a good alternative: ggplot(diamonds, aes(clarity)) + geom_bar() + facet_wrap(~ cut)

# If the x axis is ordered, using a line instead of bars is another # possibility: ggplot(diamonds, aes(clarity)) + geom_freqpoly(aes(group = cut, colour = cut))

# Dodged bar charts ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge")

# compare with ggplot(diamonds, aes(cut, fill=cut)) + geom_bar() + facet_grid(. ~ clarity)

# But again, probably better to use frequency polygons instead: ggplot(diamonds, aes(clarity, colour=cut)) + geom_freqpoly(aes(group = cut))

# Often we don't want the height of the bar to represent the # count of observations, but the sum of some other variable. # For example, the following plot shows the number of diamonds # of each colour qplot(color, data=diamonds, geom="bar")

# If, however, we want to see the total number of carats in each colour # we need to weight by the carat variable qplot(color, data=diamonds, geom="bar", weight=carat, ylab="carat")

# A bar chart used to display means meanprice <- tapply(diamonds$price, diamonds$cut, mean) cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut)) qplot(cut, meanprice)

qplot(cut, meanprice, geom="bar", stat="identity")

qplot(cut, meanprice, geom="bar", stat="identity", fill = I("grey50"))

# Another stacked bar chart example k <- ggplot(mpg, aes(manufacturer, fill=class)) k + geom_bar()

# Use scales to change aesthetics defaults k + geom_bar() + scale_fill_brewer()

k + geom_bar() + scale_fill_grey()

# To change plot order of class varible # use factor() to change order of levels mpg$class <- factor(mpg$class, levels = c("midsize", "minivan", "suv", "compact", "2seater", "subcompact", "pickup")) m <- ggplot(mpg, aes(manufacturer, fill=class)) m + geom_bar()

Bars, rectangles with bases on x-axis的更多相关文章

  1. Flot Reference flot参考文档

    Consider a call to the plot function:下面是对绘图函数plot的调用: var plot = $.plot(placeholder, data, options) ...

  2. 谈谈Python中列表、元组和数组的区别和骚操作

    一.列表(List) 1.列表的特点 列表是以方括号“[]”包围的数据集合,不同成员以“,”分隔.如 L = [1,2,3], 列表a有3个成员. 列表是可变的数据类型[可进行增删改查],列表中可以包 ...

  3. UVA 10574 - Counting Rectangles 计数

    Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular ...

  4. UVA - 10574 Counting Rectangles

    Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...

  5. 应用Apache Axis进行Web Service开发

    转自(http://tscjsj.blog.51cto.com/412451/84813) 一.概述 SOAP原意为Simple Object Access Protocol(简单对象访问协议),是一 ...

  6. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  7. 使用axis开发web service服务端

    一.axis环境搭建 1.安装环境 JDK.Tomcat或Resin.eclipse等. 2.到 http://www.apache.org/dyn/closer.cgi/ws/axis/1_4下载A ...

  8. PeopleSoft Rich Text Boxes上定制Tool Bars

      在使用PT8.50或在8.51时,你可能遇到过Rich-text编辑框.该插件使你能够格式化文本,添加颜色.链接.图片等等.下面是效果图: 如果页面中只有这么一个字段,该文本框就会有足够的空间来容 ...

  9. AXIS最佳实践

    前言: Axis是apache一个开源的webservice服务,需要web容器进行发布.本节主要用于介绍使用Axis开发webservice,包括服务端的创建.webservice的部署.客户端的调 ...

随机推荐

  1. 【Linux】撷取命令cut

    什么是撷取命令啊?说穿了,就是将一段数据经过分析后,取出我们所想要的.或者是经由分析关键词,取得我们所想要的那一行! 不过,要注意的是,一般来说,撷取信息通常是针对『一行一行』来分析的,并不是整篇信息 ...

  2. Excel提示“此工作簿包含一个或多个无法更新的链接”怎么办

    有时打开Excel文件时会弹出一个“此工作簿包含一个或多个无法更新的链接”的提示.对于初次接触这个提示的用户,可能会感到迷惑,不知道应该如何处理,这里以Excel2007为例,介绍一下这个提示出现的原 ...

  3. AME_PR采购申请单通过AME审批设定和测试(案例)

    2014-06-21 Created By BaoXinjian

  4. 整理了一份招PHP高级工程师的面试题(转)

    1. 基本知识点 HTTP协议中几个状态码的含义:1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码   说明 100   (继续) 请求者应当继续提出请求. 服务器返回此代码 ...

  5. 【JEECG_3.7.1】列表多表头的设计

    先看下多表头的设计: 在这个多表头的表单当中,我们可以按照从上到下和从左往右的划分方式,将表头划分成三行十列,分别是: 列表标签 人员信息.部门信息.工资.入职状态.创建日期.操作 名称.年龄.性别. ...

  6. Opening Default document on IIS (HTML With WebAPI)

    Question: I've a deployed ASP.NET Web API with a website on the same folder that consume it. When I ...

  7. 教你如何写thinkphp多表查询语句

    1.table()函数 thinkphp中提供了一个table()函数,具体用法参考以下语句: $list=$Demo->table('think_blog blog,think_type ty ...

  8. Chisel Tutorial(七)——模块

    下面内容根据2015-7-10版的Chisel 2.2 Tutorial整理 Chisel中的模块与Verilog HDL中模块的概念十分相似,都是用层次结构描写叙述电路.Chisel中的module ...

  9. redis make test 报错

    [root@ok redis-]# make test cd src && make test make[]: Entering directory `/usr/local/src/r ...

  10. WPF学习之X名称空间详解

    X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的.用来引导XAML代码将XAML代码编译为CLR代码. 4.1X名称空间里面到底都有些什么? x名称空间映射的是:htt ...