码迷,mamicode.com
首页 > 其他好文 > 详细

Bars, rectangles with bases on x-axis

时间:2017-03-06 13:34:42      阅读:560      评论:0      收藏:0      [点我收藏+]

标签:...   cat   compare   number   article   frequency   str   category   from   

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

标签:...   cat   compare   number   article   frequency   str   category   from   

原文地址:http://www.cnblogs.com/awishfullyway/p/6509234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!