2017-07-15 185 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軸類別標籤周圍繪製成角度的邊框的數字。任何幫助將不勝感激。謝謝。 –

相關問題