2017-07-15 184 views

回答

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

enter image description here

+0

谢谢你巴蒂斯特的快速回复。我对这个有点新,所以忍受着我。我在上面的帖子中添加了一个图表示例,以提供我需要的更多背景信息。我设法得到了一张贴图,就像张贴的照片一样,但我很难在x和y轴标签周围绘制黑色边框,而不是标题(即“Category1”,...“Category 5”和“Type A“,”D型“)。图例:https://i.stack.imgur.com/Mj7KF.jpg –

+0

再次baptiste,谢谢你的回答。我认为你提供的代码可以概括为我所需要的。只是不太了解Grobs和网格能够将其应用于axis.text.x和axis.text.y(其中包含多个标签),更不用说在顶部的x轴类别标签周围绘制成角度的边框的数字。任何帮助将不胜感激。谢谢。 –

相关问题