在 ggplot2 中显示频率和条形图

发布于 2024-08-27 09:18:54 字数 244 浏览 4 评论 0原文

我正在尝试在条形图中显示频率...好吧,我希望它们位于图表中的某处:条形图下方、条形图内、条形图上方或图例区域中。我记得(我可能是错的)它可以在 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 技术交流群。

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

发布评论

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

评论(5

病女 2024-09-03 09:18:54

geom_text 与基础图形中的 text 类似:

p + geom_bar() + stat_bin(aes(label=..count..), vjust=0, 
                          geom="text", position="identity")

如果要调整标签的 y 位置,可以使用 y= stat_bin 内的美观:例如,y=..count..+1 会将标签放在条形上方一个单位。

如果您在内部使用 geom_textstat="bin" ,上面的代码也适用。

geom_text is tha analog of text from base graphics:

p + geom_bar() + stat_bin(aes(label=..count..), vjust=0, 
                          geom="text", position="identity")

If you want to adjust the y-position of the labels, you can use the y= aesthetic within stat_bin: for example, y=..count..+1 will put the label one unit above the bar.

The above also works if you use geom_text and stat="bin" inside.

兔小萌 2024-09-03 09:18:54

一个很难做到的方法。我确信有更好的方法。

ggplot(mtcars,aes(factor(cyl))) + 
geom_bar() + 
geom_text(aes(y=sapply(cyl,function(x) 1+table(cyl)[names(table(cyl))==x]),
label=sapply(cyl,function(x) table(cyl)[names(table(cyl))==x])))

A hard way to do it. I'm sure there are better approaches.

ggplot(mtcars,aes(factor(cyl))) + 
geom_bar() + 
geom_text(aes(y=sapply(cyl,function(x) 1+table(cyl)[names(table(cyl))==x]),
label=sapply(cyl,function(x) table(cyl)[names(table(cyl))==x])))
红玫瑰 2024-09-03 09:18:54

当想要添加不同的信息时,可以执行以下操作:

ggplot(mydata, aes(x=clusterSize, y=occurence)) +
geom_bar() + geom_text(aes(x=clusterSize, y=occurence, label = mydata$otherinfo))

When wanting to add different info the following works:

ggplot(mydata, aes(x=clusterSize, y=occurence)) +
geom_bar() + geom_text(aes(x=clusterSize, y=occurence, label = mydata$otherinfo))
跨年 2024-09-03 09:18:54

另外,我发现使用一些可用的注释函数很有用:ggplot2::annotateggplot2::annotation_customcowplot::draw_label(它是 annotation_custom 的包装器)。

ggplot2::annotate 只是回收 geom 文本选项。 ggplot2::annotation_custom 或cowplot::draw_label 提供的功能对于在画布上的任何位置进行绘图更有利。

ggplot2::annotate 示例

library(ggplot2)

p <- ggplot(mtcars) + aes(factor(cyl)) + geom_bar()

# Get data from the graph
p_dt <- layer_data(p) # or ggplot_build(p)$data

p + annotate(geom = "text", label = p_dt$count, x = p_dt$x, y = 15)

或者允许 y 变化:

p + annotate(geom = "text", label = p_dt$count, x = p_dt$x, y = p_dt$y + 1)

使用 ggplot2::annotation_custom 的示例

ggplot2::annotate 有尝试在更“非常规”的地方绘制时存在局限性,正如最初所要求的那样(“图表中的某个地方”)。但是,ggplot2::annotation_custom设置裁剪,允许在画布/工作表上任何地方进行注释,如下例所示:

p2 <- p + coord_cartesian(clip = "off")
for (i in 1:nrow(p_dt)){
  p2 <- p2 + annotation_custom(grid::textGrob(p_dt$count[i]), 
                               xmin = p_dt$x[i], xmax = p_dt$x[i], ymin = -1, ymax = -1)
}
p2

示例cowplot::draw_label

cowplot::draw_labelggplot2::annotation_custom 的包装,并且稍微不那么冗长(因此)。它还需要剪裁才能在画布上的任何位置绘制。

library(cowplot)
#> Warning: package 'cowplot' was built under R version 3.5.2
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave
# Revert to default theme; see https://stackoverflow.com/a/41096936/5193830
theme_set(theme_grey())

p3 <- p + coord_cartesian(clip = "off")
for (i in 1:nrow(p_dt)){
  p3 <- p3 + draw_label(label = p_dt$count[i], x = p_dt$x[i], y = -1.8)
}
p3

请注意,draw_label 也可以与 cowplot::ggdraw< 结合使用/code>,切换到相对坐标,范围从 0 到 1(相对于整个画布,请参阅 help(draw_label) 示例)。在这种情况下,不再需要设置 coord_cartesian(clip = "off"),因为事情由 ggdraw 处理。

reprex 包 (v0.2.1) 创建于 2019 年 1 月 16 日

Alternatively, I found useful to use some of the available annotation functions: ggplot2::annotate, ggplot2::annotation_custom or cowplot::draw_label (which is a wrapper of annotation_custom).

ggplot2::annotate is just recycling the geom text option. More advantageous for plotting anywhere on the canvas are the possibilities offered by ggplot2::annotation_custom or cowplot::draw_label.

Examples with ggplot2::annotate

library(ggplot2)

p <- ggplot(mtcars) + aes(factor(cyl)) + geom_bar()

# Get data from the graph
p_dt <- layer_data(p) # or ggplot_build(p)$data

p + annotate(geom = "text", label = p_dt$count, x = p_dt$x, y = 15)

Or allow y to vary:

p + annotate(geom = "text", label = p_dt$count, x = p_dt$x, y = p_dt$y + 1)

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:

p2 <- p + coord_cartesian(clip = "off")
for (i in 1:nrow(p_dt)){
  p2 <- p2 + annotation_custom(grid::textGrob(p_dt$count[i]), 
                               xmin = p_dt$x[i], xmax = p_dt$x[i], ymin = -1, ymax = -1)
}
p2

Example with cowplot::draw_label

cowplot::draw_label is a wrapper of ggplot2::annotation_custom, and is slightly less verbose (as a consequence). It also needs clipping off to plot anywhere on the canvas.

library(cowplot)
#> Warning: package 'cowplot' was built under R version 3.5.2
#> 
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggplot2':
#> 
#>     ggsave
# Revert to default theme; see https://stackoverflow.com/a/41096936/5193830
theme_set(theme_grey())

p3 <- p + coord_cartesian(clip = "off")
for (i in 1:nrow(p_dt)){
  p3 <- p3 + draw_label(label = p_dt$count[i], x = p_dt$x[i], y = -1.8)
}
p3

Note that, draw_label can also be used in combination with cowplot::ggdraw, switching to relative coordinates, ranging from 0 to 1 (relative to the entire canvas, see examples with help(draw_label)). In that case setting coord_cartesian(clip = "off") is not required anymore as things are taken care by ggdraw.

Created on 2019-01-16 by the reprex package (v0.2.1)

你的他你的她 2024-09-03 09:18:54

如果您不限于 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.

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