2013-02-04 43 views
6

如何一鍵傳說添加到下面的情節添加按鍵傳說多直方圖中的R

enter image description here

我嘩嘩在右上角的地方有一個關鍵的傳說與兩短橫彩條,紅色的應該說「整形手術出了問題」,藍色的應該說「德國」。

我用下面的代碼以產生所述情節:

bar2 <- read.table("div/ana-mut[...]/barriers-set-2.dat", sep=" ") 
bar2val <- c(bar2$V1, bar2$V2) 
bar3 <- read.table("div/ana-mut[...]/barriers-set-3.dat", sep=" ") 
bar3val <- c(bar3$V1, bar3$V2) 
p1 <- hist(subset(bar2val, bar2val < 30), breaks=30) 
p2 <- hist(subset(bar3val, bar3val < 30), breaks=30) 
plot(p1, col=rgb(1,0,0,8/9), main="Barrier distribution", xlab="Barrier [kcal/mol]", ylab="Mutant count") 
plot(p2, col=rgb(0,0,1,8/9), add=T) 

任何提示,將不勝感激。

+1

幾個百分點。首先請讓你的問題[reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。另外,告訴我們你已經嘗試過了。 – csgillespie

回答

15

傳說命令就可以了:

legend("topright", c("Germany", "Plastic"), col=c("blue", "red"), lwd=10) 

要獲得兩個短橫彩條只需使用一個標準線,但增加線路厚度。羅蘭指出的那樣,你也可以使用fill參數:

legend("topright", c("Germany", "Plastic"), fill=c("blue", "red")) 

詳情請參閱?legend

enter image description here

+1

對於直方圖,我會使用'fill'參數來繪製盒子,而不是在圖例中有線條。 – Roland

+0

@羅蘭好點,謝謝。 – csgillespie

+0

謝謝csgillespie。這應該足以滿足我的需求。 – TMOTTM

2

這裏是一種替代解決方案(請參閱下面的代碼)

enter image description here

# some semi-random data … 
df <- structure(list(Germany = c(1L, 3L, 6L, 1L, 2L), Plastic = c(2L, 
5L, 4L, 2L, 3L)), .Names = c("Germany", "Plastic"), class = "data.frame", row.names = c(NA, 
-5L)) 

# Expand right side of clipping rect to make room for the legend 
par(xpd=T, mar=par()$mar+c(0,0,0,4)) 

# Graph data (transposing the matrix) using heat colors, 
# put 10% of the space between each bar, and make labels 
# smaller with horizontal y-axis labels 
barplot(t(df), main="Barrier distribution", xlab="Barrier [kcal/mol]", ylab="Mutant count", 
    col=c("blue", "red"), space=0.1, cex.axis=0.8, las=1, 
    names.arg=c("Mon","Tue","Wed","Thu","Fri"), cex=0.8) 

# Place the legend at (4,9) using blue and red 
legend(4, 9, names(df), lwd=4, col=c("blue", "red")) 

# Restore default clipping rect 
par(mar=c(5, 4, 4, 2) + 0.1) 
+0

@TMOTTM,這個答案在任何方面有幫助嗎? –