2013-10-06 44 views
7

我使用ggplot2(版本0.9.3.1)在R(版本R版本3.0.1(2013-05-16))中生成了一個簡單圖表,顯示了一組數據的相關係數。目前,繪圖右側的圖例顏色條是整個繪圖尺寸的一小部分。我怎樣才能讓ggplot2中的圖例與我的情節一樣高?

我希望圖例顏色條與圖的高度相同。我認爲我可以使用legend.key.height來做到這一點,但我發現情況並非如此。我調查了grid程序包unit函數,發現裏面有一些標準化的單元,但是當我嘗試它們時(unit(1, "npc")),顏色條顯得太高而離開了頁面。

我怎樣才能讓圖例與情節本身一樣高呢?

一個完整的自包含的例子如下:

# Load the needed libraries 
library(ggplot2) 
library(grid) 
library(scales) 
library(reshape2) 

# Generate a collection of sample data 
variables = c("Var1", "Var2", "Var3") 
data = matrix(runif(9, -1, 1), 3, 3) 
diag(data) = 1 
colnames(data) = variables 
rownames(data) = variables 

# Generate the plot 
corrs = data 
ggplot(melt(corrs), aes(x = Var1, y = Var2, fill = value)) + 
    geom_tile() + 
    geom_text(parse = TRUE, aes(label = sprintf("%.2f", value)), size = 3, color = "white") + 
    theme_bw() + 
    theme(panel.border = element_blank(), 
     axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1), 
     aspect.ratio = 1, 
     legend.position = "right", 
     legend.key.height = unit(1, "inch")) + 
     labs(x = "", y = "", fill = "", title = "Correlation Coefficients") + 
     scale_fill_gradient2(limits = c(-1, 1), expand = c(0, 0), 
          low = muted("red"), 
          mid = "black", 
          high = muted("blue")) 

enter image description here

+2

請發表最小的自包含重複的例子, – baptiste

+0

會做不久....編輯 –

+0

好了,問題有一個完整的可運行的例子 –

回答

1

它似乎很棘手,我得到的最接近的是這樣的,

## panel height is 1null, so we work it out by subtracting the other heights from 1npc 
## and 1line for the default plot margins 

panel_height = unit(1,"npc") - sum(ggplotGrob(plot)[["heights"]][-3]) - unit(1,"line") 
plot + guides(fill= guide_colorbar(barheight=panel_height)) 

不幸的是,垂直理由是有點關閉。

+0

漂亮durned接近....也許有點更多的調整,但我繼續前進,並將其標記爲回答。感謝指針。 –

9

這很混亂,但基於this answer,深入研究ggplot grob,可以精確定位圖例。

# Load the needed libraries 
library(ggplot2) 
library(gtable) # 
library(grid) 
library(scales) 
library(reshape2) 

# Generate a collection of sample data 
variables = c("Var1", "Var2", "Var3") 
data = matrix(runif(9, -1, 1), 3, 3) 
diag(data) = 1 
colnames(data) = variables 
rownames(data) = variables 

# Generate the plot 
corrs = data 
plot = ggplot(melt(corrs), aes(x = Var1, y = Var2, fill = value)) + 
    geom_tile() + 
    theme_bw() + 
    theme(panel.border = element_blank()) + 
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + 
    theme(aspect.ratio = 1) + 
    # theme(legend.position = "right", legend.key.height = unit(1, "inch")) + 
    labs(x = "", y = "", fill = "", title = "Correlation Coefficients") + 
    scale_fill_gradient2(limits = c(-1, 1), breaks = c(-1, -.5, 0, .5, 1), expand = c(0,0), 
     low = muted("red"), mid = "black", high = muted("blue")) + # Modified line 
    geom_text(parse = TRUE, aes(label = sprintf("%.2f", value)), size = 3, color = "white") + 
    scale_x_discrete(expand = c(0,0)) + # New line 
    scale_y_discrete(expand = c(0,0)) # New line 
plot 

# Get the ggplot grob 
gt = ggplotGrob(plot) 

# Get the legend 
leg = gtable_filter(gt, "guide-box") 

# Raster height 
leg[[1]][[1]][[1]][[1]][[1]][[2]]$height = unit(1, "npc") 

# Positions for labels and tick marks - five breaks, therefore, five positions 
pos = unit.c(unit(0.01,"npc"), unit(.25, "npc"), unit(.5, "npc"), unit(.75, "npc"), unit(.99, "npc")) 

# Positions the labels 
leg[[1]][[1]][[1]][[1]][[1]][[3]]$y = pos 

# Positions the tick marks 
leg[[1]][[1]][[1]][[1]][[1]][[5]]$y0 = pos 
leg[[1]][[1]][[1]][[1]][[1]][[5]]$y1 = pos 

# Legend key height ? 
leg[[1]][[1]][[1]][[1]]$heights = unit.c(rep(unit(0, "mm"), 3), 
             unit(1, "npc"), 
             unit(0, "mm")) 
# Legend height 
leg[[1]][[1]]$heights[[3]] = sum(rep(unit(0, "mm"), 3), 
           unit(1, "npc"), 
           unit(0, "mm")) 

# grid.draw(leg) # Check on heights and y values 

# gtable_show_layout(gt) # Manually locate position of legend in layout 
gt.new = gtable_add_grob(gt, leg, t = 6, l = 8) 

# Draw it 
grid.newpage() 
grid.draw(gt.new) 

enter image description here