如何在x或y轴标签周围绘制框/边框?
问题描述:
R有没有在x或y轴标签周围绘制框/边框的方法,可能是成角度的标签?如何在x或y轴标签周围绘制框/边框?
我一直在使用ggplot
创建平铺图表,发现周围放置在标签中的数据本身(通过geom_label
代码:Set ggplot2 label background color但不是围绕轴标签本身
图表示例:
答
library(grid)
element_custom <- function() {
structure(list(), class = c("element_custom", "element_text"))
}
element_grob.element_custom <- function(element, label="", ...) {
tg <- textGrob(label)
padding <- unit(1,"line")
rg <- rectGrob(width=grobWidth(tg)+padding, height=grobHeight(tg)+padding)
gTree(children=gList(rg, tg), height=grobHeight(tg) + padding, cl="custom_axis")
}
heightDetails.custom_axis <- function(x) x$height + unit(2,"mm") # fudge
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_line() +
labs(x= "Axis title")+
(theme_grey() %+replace% theme(axis.title.x = element_custom()))
谢谢你巴蒂斯特的快速回复。我对这个有点新,所以忍受着我。我在上面的帖子中添加了一个图表示例,以提供我需要的更多背景信息。我设法得到了一张贴图,就像张贴的照片一样,但我很难在x和y轴标签周围绘制黑色边框,而不是标题(即“Category1”,...“Category 5”和“Type A“,”D型“)。图例:https://i.stack.imgur.com/Mj7KF.jpg –
再次baptiste,谢谢你的回答。我认为你提供的代码可以概括为我所需要的。只是不太了解Grobs和网格能够将其应用于axis.text.x和axis.text.y(其中包含多个标签),更不用说在顶部的x轴类别标签周围绘制成角度的边框的数字。任何帮助将不胜感激。谢谢。 –