2014-01-24 3 views
-1

ggplot 2에서 전/후부에 대한 범례를 만들고 싶습니다. knitr을 사용하고 있으므로 전송할 수 있어야하지만 문제가되지 않아야합니다. 다음은 내가 가지고있는 코드 :ggplot2에서 전후방에 범례를 만들려면 어떻게해야합니까?

<<echo=FALSE,message=FALSE,cache=FALSE,include=TRUE,fig.height=5,fig.pos="h!", 
warning=FALSE>>= 
require(ggplot2) 
x <- seq(0, 1, len = 100) 
y <- seq(0,6,len=100) 

p <- qplot(x, geom = "blank") 
Prior <- stat_function(aes(x = x, y = y), fun = dbeta, colour="red", n = 1000, 
        args = list(shape1 = 3, shape2 = 7)) 
Posterior <- stat_function(aes(x = x, y = ..y..), fun = dbeta, colour="blue", 
n = 1000,args = list(shape1 = 7, shape2 = 23)) 
p + Prior + Posterior 
@ 

내가 몇 가지를 시도했지만 내가 가장 좋은 방법을 알아낼 수 없습니다. 감사!

답변

0

aes(...)colour=을 넣으면 ggplot이 색상 표를 만들어 범례를 자동으로 만듭니다.

p <- qplot(x, geom = "blank") 
Prior <- stat_function(aes(x = x, y = y,color="#FF0000"), fun = dbeta, n = 1000, 
         args = list(shape1 = 3, shape2 = 7)) 
Posterior <- stat_function(aes(x = x, y = y,color="#0000FF"), fun = dbeta, 
          n = 1000,args = list(shape1 = 7, shape2 = 23)) 
p + Prior + Posterior + 
    scale_color_discrete("Distibution",labels=c("Prior","Posterior")) 

관련 문제