2016-03-24 64 views
1
barplot(counts, main="title", 
     xlab=group, col=rainbow(20), 
     legend = rownames(counts), 
     beside=TRUE, xlim=c(1,30)) 
abline(v=mean(df$ddd), col="red", lwd=2.5) 

我的問題是 - 如何添加abline到圖例?我試圖從barplot區域拋出這個傳說,但是它沒有起作用在barplot RStudio添加abline圖例

回答

1

不知道更多關於您的數據的信息,很難複製您正在尋找的內容,但我認爲您可能會發現R的ggplot2包有用:

library(ggplot2) 

ggplot(data=iris) + geom_bar(aes(x=iris$Sepal.Length,fill=iris$Species)) + 
     #Add abline - specifiy color aes so it appears in the legend 
     geom_abline(aes(intercept=0,slope=1,color="Line description"),size=2.5,show_guide=TRUE) + 

     #Override the lines that would appear in "fill" portion of legend - the part of the legend that refers to species 
     guides(fill = guide_legend(override.aes = list(linetype = 0), title="[TITLE OF FILL]"), 
      color = guide_legend(title="[TITLE OF LINE]")) + 

     #Change color of line to what you actually want 
     scale_color_manual(values="#CC6666") 

我們基本上是「絕招」 ggplot到把geom_abline在圖例中,然後從那裏我們可以更改標題,描述和線路的顏色。

上面的代碼給我們:

enter image description here