如何使用 ggplot 创建并排条形图(针对多个系列)?

发布于 2024-09-17 17:52:08 字数 1585 浏览 0 评论 0原文

我有两组数据(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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光匆匆的小流年 2024-09-24 17:52:09

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

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

说明书上有,搜索“闪避”即可。另外,如果 y 值给出了条形的高度,您可能需要一个“条形”几何图形。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文