更新时间:2023-11-10 10:03:22
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)
g Tree(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 =轴标题)+
(theme_grey()%+替换%theme(axis.title.x = element_custom()))
Is there a way in R to draw boxes/borders around x or y axis labels, possibly angled labels?
I've been using ggplot
to create tile charts and found code that places around labels in the data itself (through geom_label
: Set ggplot2 label background color but not around labels in the axes themselves.
Chart Example:
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()))