像这样上一张海报 https://stackoverflow.com/questions/7611691/relative-positioning-of-geom-text-in-ggplot2,我也在用geom_text
在 gglot2 中注释绘图。我想将这些注释定位在相对坐标(构面 H & W 的比例)而不是数据坐标中。对于大多数图来说都很简单,但就我而言,我正在处理直方图。我确信有关 y 比例的相关信息必须潜伏在绘图对象的某处(添加后geom_histogram
),但我不知道在哪里。
我的问题:如何从包含的多面 ggplot2 对象中读取最大条形高度geom_histogram
?有人可以帮忙吗?
尝试这个:
library(plyr)
library(scales)
p <- ggplot(mtcars, aes(mpg)) + geom_histogram(aes(y = ..density..)) + facet_wrap(~am)
r <- print(p)
# in data coordinate
(dc <- dlply(r$data[[1]], .(PANEL), function(x) max(x$density)))
(mx <- dlply(r$data[[1]], .(PANEL), function(x) x[which.max(x$density), ]$x))
# add annotation (see figure below)
p + geom_text(aes(x, y, label = text),
data = data.frame(x = unlist(mx), y = unlist(dc), text = LETTERS[1:2], am = 0:1),
colour = "red", vjust = 0)
# scale range
(yr <- llply(r$panel$ranges, "[[", "y.range"))
# in relative coordinates
(rc <- mapply(function(d, y) rescale(d, from = y), dc, yr))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)