【发布时间】:2011-02-02 20:40:05
【问题描述】:
我正在尝试在条形图中显示频率...嗯,我希望它们在图表中的某处:在条形下方、条形内、条形上方或图例区域中。我记得(我可能错了)它可以在ggplot2 中完成。这可能很容易……至少看起来很容易。代码如下:
p <- ggplot(mtcars)
p + aes(factor(cyl)) + geom_bar()
有没有机会在图表中嵌入频率?
【问题讨论】:
我正在尝试在条形图中显示频率...嗯,我希望它们在图表中的某处:在条形下方、条形内、条形上方或图例区域中。我记得(我可能错了)它可以在ggplot2 中完成。这可能很容易……至少看起来很容易。代码如下:
p <- ggplot(mtcars)
p + aes(factor(cyl)) + geom_bar()
有没有机会在图表中嵌入频率?
【问题讨论】:
另外,我发现使用一些可用的注释函数很有用:ggplot2::annotate、ggplot2::annotation_custom 或 cowplot::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 在尝试在更多“非传统”位置绘制时存在局限性,正如最初询问的那样(“somewhere in the graph”)。但是,ggplot2::annotation_custom 与setting clipping off 结合使用,允许在画布/工作表上anywhere 进行注释,如下例所示:
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_label 是ggplot2::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结合使用,切换到相对坐标,范围从0到1(相对于整个画布,见help(draw_label)的例子)。在这种情况下,不再需要设置 coord_cartesian(clip = "off"),因为事情由 ggdraw 负责。
由reprex package (v0.2.1) 于 2019 年 1 月 16 日创建
【讨论】:
当想要添加不同的信息时,以下工作:
ggplot(mydata, aes(x=clusterSize, y=occurence)) +
geom_bar() + geom_text(aes(x=clusterSize, y=occurence, label = mydata$otherinfo))
【讨论】:
datasets 包(或 CRAN 存储库中可用的其他包)中可用数据的示例来复制此答案?我怀疑条形图可以用“按原样”指定的y 变量绘制...
geom_text 是基本图形中text 的模拟:
p + geom_bar() + stat_bin(aes(label=..count..), vjust=0,
geom="text", position="identity")
如果要调整标签的 y 位置,可以使用 stat_bin 中的 y= 美学:例如,y=..count..+1 会将标签放在条形上方一个单位。
如果您在内部使用geom_text 和stat="bin",上述方法也有效。
【讨论】:
..count.. 是包含 stat_bin 自动创建的 bin 频率的变量的名称。所以它前后的两个句点是变量名的一部分
Error: stat_count requires the following missing aesthetics: x。但是,添加 aes(factor(cyl)) 并将 stat_bin 更改为 stat_count,就像在 p + aes(factor(cyl)) + geom_bar() + stat_count(aes(label=..count..), vjust=0, geom="text", position="identity") 中一样。
+ geom_text(aes(label=..count..), stat="count") 工作(而不是 stat="bin")
很难做到。我确信有更好的方法。
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])))
【讨论】:
如果您不限于 ggplot2,您可以使用基本图形中的 ?text 或 plotrix 包中的 ?boxed.labels。
【讨论】: