如何用ggplot创建并排的条形图(用于多个系列)? [英] How to create side-by-side bar charts (for multiple series) with ggplot?

查看:1300
本文介绍了如何用ggplot创建并排的条形图(用于多个系列)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我试过了

  qplot(x,y,data = mydata,col = location,geom =直方图)

但它会产生堆叠的酒吧,而不是并排。然后我查看了ggplot2文档,没有发现任何其他可以使用的geom(见下面的完整列表)。

ggplot2不可能这样做吗?




$ b

 名称说明
abline - 行,由斜率和截距指定
区域 - 面积图
bar - 酒吧,以y轴为基准的矩形
空白 - 空白,不绘制
boxplot - 盒须plot
contour - 在2d
横杆上显示3d曲面的轮廓 - 由水平线表示的中间的空心条
density - 显示平滑密度估计值
density_2d - 来自2d的轮廓线密度估计
误差线 - 误差线
直方图 - 直方图
线 - 水平线,
间隔 - 所有间隔(范围)几何基数
抖动 - 点抖动减少重叠绘图
line - 按x值的顺序连接观察值
linerange - 由垂直线表示的区间
path - 连接观察点按原始顺序排列
点 - 点与散点图
点间距 - 用垂直线表示的间隔,中间
多边形中的点
- 多边形,a填充路径
分位数 - 从分位数回归中添加分位数线
功能区 - 色带,y连续x值的范围
rug - 边际地毯块
段 - 单线段
平滑 - 添加平滑条件意味着
步骤 - 通过楼梯连接观察值
text - 文本注释
tile - 尽可能密集的平铺图,假定每个图块的大小相同
vline - 行,垂直


解决方案

c $ c> position 这里默认为 stack 的参数。使用:

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

在手册中,只需搜索闪避即可。此外,如果 y 值给出了栏的高度,那么您可能需要一个栏几何。


I have two sets of data (3 columns: x=categorical, y = numerical, l = location) and I would like to create a bar chart with the categories on the x axis and, for each value of the category, two vertical bars, coloured differently, with the y values for each location. By the default, Excel/OpenOffice produce this kind of chart.

I tried

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

but it produces stacked bars, not side by side. I then looked in the ggplot2 documentation and didn't find any other geom I could use (see below for full list).

Is this not possible with ggplot2?

Thanks in advance.

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

解决方案

There is a position argument that defaults to stack here. Use:

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

It is in the manual, just search for "dodge". Also, you probably want a "bar" geom if the y values give the height of the bar.

这篇关于如何用ggplot创建并排的条形图(用于多个系列)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆