2016-07-27 60 views
1

我是新來的,所以如果我忘記了,請原諒我。 我試圖弄到像下面的圖片。但我在兩點掙扎:我的線不直接從y軸開始,我想在x軸的測量下面和x軸的標籤兩個標籤作爲vline的圖例。我試圖用annotation_custom來獲取它,但是這將它放置在繪圖本身中,但是我希望它在x軸上或繪圖的頂部,而不會寫入圖形本身。x軸上的附加標籤作爲分隔符的圖例

到目前爲止,我用下面的代碼:

require(reshape) 
library(ggplot2) 

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

ggplot(dfm, aes(x=factor(variable), y=value, colour=user, group=user)) + 
    geom_line() + 
    scale_x_discrete(breaks=flevels, labels=flevels) 

這是我怎麼想擁有它(的標籤的Class1和Class2中)

enter image description here

我真的並欣賞你幫幫我。

回答

0

要直接啓動線在y軸你需要把paramater展開到scale_x_discrete功能,如:

scale_x_discrete(breaks=flevels, labels=flevels, expand = c(0,0)) 

您可以添加一個傳說,如:

theme(legend.position = 'bottom') 

的只要您的數據中有三位用戶具有相同的值,結果就會是三個標籤。我認爲通過消除用戶2對數據進行子集化將是獲得兩個標籤的最簡單方法。

+0

謝謝advek,已經幫助了很多。我不知道。你也有一個想法如何獲得標籤到分隔符 – Momina

+0

我編輯我的原始答案,希望這是你在找什麼。 – advek88

+0

我不想改變我的傳奇。這就像我的問題。 x軸標籤(Meas1,Meas2,Meas3)和x標籤(因子(變量))之間有兩個標籤。我想要這樣,所以你知道你在class1的右邊有class1,在右邊有class2 – Momina

0

想到一些可能性。我在下面列出三個。在所有三項中,我將「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可以跨越-InfInf。有一點點的嘗試和錯誤,以確保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插入該行。 tl指的是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) 

enter image description here