如何使用ggplot创建多系列并列条形图?

5

我有两组数据(3列:x=分类,y=数值,l=位置),我想创建一个条形图,x轴上为类别,并且对于每个类别的值,使用不同颜色的两个垂直条形图,显示每个位置的y值。默认情况下,Excel/OpenOffice会生成这种类型的图表。

我尝试过

qplot (x,y,data=mydata,col=location, geom="histogram")

但它生成的是堆叠条形图,而不是并排的。然后我查看了ggplot2文档,并没有找到其他可用的几何对象(请参见下面的完整列表)。

使用ggplot2不可能实现这个吗?

提前致谢。

Name Description
abline - Line, specified by slope and intercept
area - Area plots
bar - Bars, rectangles with bases on y-axis
blank - Blank, draws nothing
boxplot - Box-and-whisker plot
contour - Display contours of a 3d surface in 2d
crossbar - Hollow bar with middle indicated by horizontal line
density - Display a smooth density estimate
density_2d - Contours from a 2d density estimate
errorbar - Error bars
histogram - Histogram
hline - Line, horizontal
interval - Base for all interval (range) geoms
jitter - Points, jittered to reduce overplotting
line - Connect observations, in order of x value
linerange - An interval represented by a vertical line
path - Connect observations, in original order
point - Points, as for a scatterplot
pointrange - An interval represented by a vertical line, with a point
in the middle
polygon - Polygon, a filled path
quantile - Add quantile lines from a quantile regression
ribbon - Ribbons, y range with continuous x values
rug - Marginal rug plots
segment - Single line segments
smooth - Add a smoothed condition mean
step - Connect observations by stairs
text - Textual annotations
tile - Tile plot as densely as possible, assuming that every tile is the same size
vline - Line, vertical

1
geom 是 geometric object 的缩写,而不是 geometry。 - hadley
我已经纠正了问题。感谢您创建ggplot2! - wishihadabettername
1个回答

4

这里有一个名为position的参数,默认值为stack。使用:

qplot (x,y,data=mydata,col=location, geom="bar", position="dodge") 

手册中有,只需搜索“dodge”。此外,如果y值给出条形图的高度,则可能需要一个“bar”几何对象。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接