如何在x或y轴标签周围绘制框/边框? [英] How to draw boxes/borders around x or y axis labels?

查看:279
本文介绍了如何在x或y轴标签周围绘制框/边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R中是否有方法在x或y轴标签周围绘制框/边框,可能是倾斜的标签?

我一直使用 ggplot 来创建瓦片图表并找到在数据本身附近放置标签的代码通过 geom_label

解决方案

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

这篇关于如何在x或y轴标签周围绘制框/边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆