作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 ggplot2
中有没有办法,要么
geom_text
将文本居中放置在绘图区域的中间例子
testData <- data.table(a = c(1,2,3,4), b=rnorm(100, 1, 3), c=rnorm(100))
ggplot(testData) + geom_point(aes(x=a, y = b)) + geom_text(aes(x=a, y = 0, label="label"))
我想避免必须手动设置 y 轴的范围,因为我会自动生成大量图表并且更愿意让 ggplot2
确定要使用的正确范围。
最佳答案
这是你想要的吗:
g1 <- ggplot(testData) +
geom_point(aes(x = a, y = b)) +
geom_text(aes(x = a, y = mean(range(b)), label="label"))
g1
Q1,如果你想访问绘图区域的范围:
# build plot object for rendering
gg1 <- ggplot_build(g1)
gg1$panel$ranges[[1]]$x.range
gg1$panel$ranges[[1]]$y.range
# mid-point of y-range from plot object
mean(gg1$panel$ranges[[1]]$y.range)
# [1] 0.5517525
# mid-point used in plot above
with(testData, y = mean(range(b)))
# [1] 0.5517525
关于r - 有没有办法在 ggplot2 中将文本居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19103646/
我是一名优秀的程序员,十分优秀!