2
R有沒有在x或y軸標籤周圍繪製框/邊框的方法,可能是成角度的標籤?如何在x或y軸標籤周圍繪製框/邊框?
我一直在使用ggplot
創建平鋪圖表,發現周圍放置在標籤中的數據本身(通過geom_label
代碼:Set ggplot2 label background color但不是圍繞軸標籤本身
圖表示例:
R有沒有在x或y軸標籤周圍繪製框/邊框的方法,可能是成角度的標籤?如何在x或y軸標籤周圍繪製框/邊框?
我一直在使用ggplot
創建平鋪圖表,發現周圍放置在標籤中的數據本身(通過geom_label
代碼:Set ggplot2 label background color但不是圍繞軸標籤本身
圖表示例:
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軸標籤周圍繪製黑色邊框,而不是標題(即「Category1」,...「Category 5」和「Type A「,」D型「)。圖例:https://i.stack.imgur.com/Mj7KF.jpg –
再次baptiste,謝謝你的回答。我認爲你提供的代碼可以概括爲我所需要的。只是不太瞭解Grobs和網格能夠將其應用於axis.text.x和axis.text.y(其中包含多個標籤),更不用說在頂部的x軸類別標籤周圍繪製成角度的邊框的數字。任何幫助將不勝感激。謝謝。 –