在 ggplot2 中显示频率和条形图
我正在尝试在条形图中显示频率...好吧,我希望它们位于图表中的某处:条形图下方、条形图内、条形图上方或图例区域中。我记得(我可能是错的)它可以在 ggplot2 中完成。这可能是一个简单的问题……至少看起来很容易。代码如下:
p <- ggplot(mtcars)
p + aes(factor(cyl)) + geom_bar()
我是否有机会将频率嵌入到图表中?
I'm trying to display frequencies within barplot ... well, I want them somewhere in the graph: under the bars, within bars, above bars or in the legend area. And I recall (I may be wrong) that it can be done in ggplot2
. This is probably an easy one... at least it seems easy. Here's the code:
p <- ggplot(mtcars)
p + aes(factor(cyl)) + geom_bar()
Is there any chance that I can get frequencies embedded in the graph?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
geom_text
与基础图形中的text
类似:如果要调整标签的 y 位置,可以使用
y=
stat_bin
内的美观:例如,y=..count..+1
会将标签放在条形上方一个单位。如果您在内部使用
geom_text
和stat="bin"
,上面的代码也适用。geom_text
is tha analog oftext
from base graphics:If you want to adjust the y-position of the labels, you can use the
y=
aesthetic withinstat_bin
: for example,y=..count..+1
will put the label one unit above the bar.The above also works if you use
geom_text
andstat="bin"
inside.一个很难做到的方法。我确信有更好的方法。
A hard way to do it. I'm sure there are better approaches.
当想要添加不同的信息时,可以执行以下操作:
When wanting to add different info the following works:
另外,我发现使用一些可用的注释函数很有用:
ggplot2::annotate
、ggplot2::annotation_custom
或cowplot::draw_label
(它是annotation_custom
的包装器)。ggplot2::annotate 只是回收 geom 文本选项。 ggplot2::annotation_custom 或cowplot::draw_label 提供的功能对于在画布上的任何位置进行绘图更有利。
ggplot2::annotate
示例或者允许
y
变化:使用
ggplot2::annotation_custom
的示例ggplot2::annotate
有尝试在更“非常规”的地方绘制时存在局限性,正如最初所要求的那样(“图表中的某个地方”)。但是,ggplot2::annotation_custom
与设置裁剪,允许在画布/工作表上任何地方进行注释,如下例所示:示例
cowplot::draw_label
cowplot::draw_label
是ggplot2::annotation_custom
的包装,并且稍微不那么冗长(因此)。它还需要剪裁才能在画布上的任何位置绘制。请注意,
draw_label
也可以与cowplot::ggdraw< 结合使用/code>,切换到相对坐标,范围从 0 到 1(相对于整个画布,请参阅
,因为事情由 ggdraw 处理。help(draw_label)
示例)。在这种情况下,不再需要设置 coord_cartesian(clip = "off")由 reprex 包 (v0.2.1) 创建于 2019 年 1 月 16 日
Alternatively, I found useful to use some of the available annotation functions:
ggplot2::annotate
,ggplot2::annotation_custom
orcowplot::draw_label
(which is a wrapper ofannotation_custom
).ggplot2::annotate
is just recycling the geom text option. More advantageous for plotting anywhere on the canvas are the possibilities offered byggplot2::annotation_custom
orcowplot::draw_label
.Examples with
ggplot2::annotate
Or allow
y
to vary:Example with
ggplot2::annotation_custom
The
ggplot2::annotate
has limitations when trying to plot in more "unconventional" places, as it was asked originally ("somewhere in the graph"). However,ggplot2::annotation_custom
in combination with setting clipping off, allows annotation anywhere on the canvas/sheet, as the below example shows:Example with
cowplot::draw_label
cowplot::draw_label
is a wrapper ofggplot2::annotation_custom
, and is slightly less verbose (as a consequence). It also needs clipping off to plot anywhere on the canvas.Note that,
draw_label
can also be used in combination withcowplot::ggdraw
, switching to relative coordinates, ranging from 0 to 1 (relative to the entire canvas, see examples withhelp(draw_label)
). In that case settingcoord_cartesian(clip = "off")
is not required anymore as things are taken care byggdraw
.Created on 2019-01-16 by the reprex package (v0.2.1)
如果您不限于 ggplot2,您可以使用基本图形中的 ?text 或plotrix 包中的 ?boxed.labels 。
If you are not restricted to ggplot2, you could use ?text from base graphics or ?boxed.labels from the plotrix package.