How to draw boxes/borders around x or y axis labels?

狂风中的少年 提交于 2019-12-01 09:11:10

问题


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:


回答1:


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()))



来源:https://stackoverflow.com/questions/45123684/how-to-draw-boxes-borders-around-x-or-y-axis-labels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!