想到一些可能性。我在下面列出三個。在所有三項中,我將「1類」和「2類」標籤放在圖表的頂部。但我不確定你想如何或在哪裏定位「1級」和「2級」之間的界限。我把它定位在圖表的一半以上。
第一種是最簡單的。它使用annotate
來定位標籤。據我所知,沒有簡單的方法可以劃出一條短線來劃分兩個地區。在這裏,我在圖表上畫了一條線,但因爲它穿過了圖表面板,我認爲這是一種分心。
第二個構建包含兩個標籤和一個短線段的grob,然後使用annotation_custom
定位grob。第三個也構建了grob(儘管grob的構造有點簡單),然後使用gtable
包中的函數定位grob。
下面更多評論。
# Your data
require(reshape)
user <- gl(3, 1)
Meas1 <- c(0.7, 0.3, 0.3)
Meas2 <- c(0.7, 0.3, 0.3)
Meas3 <- c(0.2, 0.4, 0.4)
group <- c(3, 2, 2)
df <- data.frame(user=user, Meas1=Meas1, Meas2=Meas2, Meas3=Meas3, group=group)
dfm <- melt(df, id.vars = c("user", "group"))
flevels = as.vector.factor(unique(dfm$variable))
首先方法:annotate
標籤被定位在情節面板的頂部,然後使用vjust
移動情節面板外的文本。爲了給標籤留出空間,頂部的情節邊界被擴大了。由於正在繪圖面板外繪製文本,因此需要關閉對繪圖面板的剪切。
library(ggplot2)
library(grid)
# The plot
p = ggplot(dfm, aes(x = factor(variable), y = value, colour = user, group = user)) +
geom_line() +
scale_x_discrete(breaks = flevels, labels = flevels) +
theme(plot.margin = unit(c(1.5, .1, .1, .1), "lines"))
# plus the annotations
p = p + annotate("text", x = c(1.35, 2.85), y = Inf, label = c("Class 1", "Class 2"), vjust = -.75) +
annotate("segment", x = 2.2, xend = 2.2, y = -Inf, yend = Inf)
# Turn off clipping
gp = ggplotGrob(p)
gp$layout[gp$layout$name == "panel", "clip"] = "off"
# Draw the plot
grid.newpage()
grid.draw(gp)
方法二:annotation_custom
有幾種方法的元素的位置。在這裏我做了定位在建設的溝通,然後annotation_custom
可以跨越-Inf
至Inf
。有一點點的嘗試和錯誤,以確保y座標正確。另外,因爲grob被繪製在繪圖面板之外,所以頂部繪圖邊距需要加寬,並且需要關閉對面板的裁剪。
xpos = .6 # of the demarcation between the two labels; in npc units
# Construct the grob
lab1 = textGrob("Class 1", x = .5 * xpos, y = 1.02)
lab2 = textGrob("Class 2", x = .5 + .5 * xpos, y = 1.02)
seg = linesGrob(x = c(xpos, xpos), y = c(1, 1.04))
labs = gTree(children = gList(lab1, lab2, seg))
p = ggplot(dfm, aes(x = factor(variable), y = value, colour = user, group = user)) +
geom_line() +
scale_x_discrete(breaks = flevels, labels = flevels) +
theme(plot.margin = unit(c(1.5, .1, .1, .1), "lines")) +
annotation_custom(labs, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
gp = ggplotGrob(p)
gp$layout[gp$layout$name == "panel", "clip"] = "off"
grid.newpage()
grid.draw(gp)
第三種方法:gtable
功能
的GROB構造,但需要擔心y座標。 gtable
函數將一行添加到gtable的頂部(即圖),然後將grob插入該行。 t
和l
指的是gtable佈局中的行和列。 gtable_show_layout(gp)
將顯示佈局。另請參閱gtable幫助頁面(?gtable_add_rows
和?gtable_add_grob
)
library(gtable)
xpos = .6 # in npc units
lab1 = textGrob("Class 1", x = .5 * xpos)
lab2 = textGrob("Class 2", x = .5 + .5 * xpos)
seg = linesGrob(x = c(xpos, xpos))
labs = gTree(children = gList(lab1, lab2, seg))
p = ggplot(dfm, aes(x = factor(variable), y = value, colour = user, group = user)) +
geom_line() +
scale_x_discrete(breaks = flevels, labels = flevels)
gp = ggplotGrob(p)
row = 2
gp = gtable_add_rows(gp, unit(2, "grobheight", lab1), row)
l = gp$layout[grepl("panel", gp$layout$name), ]$l
gp = gtable_add_grob(gp, labs, t = row + 1, l = l)
grid.newpage()
grid.draw(gp)
謝謝advek,已經幫助了很多。我不知道。你也有一個想法如何獲得標籤到分隔符 – Momina
我編輯我的原始答案,希望這是你在找什麼。 – advek88
我不想改變我的傳奇。這就像我的問題。 x軸標籤(Meas1,Meas2,Meas3)和x標籤(因子(變量))之間有兩個標籤。我想要這樣,所以你知道你在class1的右邊有class1,在右邊有class2 – Momina