2016-03-24 10 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) 

私の質問は - 凡例にアブラインを追加するにはどうすればいいですか?私はバープロット領域から伝説を投げようとしましたが、何かをしましたが動作しませんでしたバープロットの凡例にアブラインを追加するRStudio

答えて

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

関連する問題