2014-10-17 11 views
0

범례에서 레이블을 변경하고 싶습니다.ggplot2 범례의 레이블 변경

spi<-read.csv("spill.csv",as.is=TRUE) 
attach(spi) 
spi$date<-as.Date(spi$date) 
str(spi) 
data.frame: 3184 obs. of 3 variables: 
$ data1: chr "oil to asset" "oil to asset" "oil to asset" "oil to asset" ... 
$ date : Date, format: "2007-01-10" "2007-01-11" ... 
$ sp : num 7.7 7.95 7.54 7.61 7.67 ... 

cont1 <- ggplot(spi,aes(x=date,y=sp,linetype=data1))+geom_line() + 
      expand_limits(max(spi$sp), y=c(0,80)) + 
      labs(x = "", y = "") + 
      scale_y_continuous(breaks=seq(0,80,10)) + 
      scale_linetype_manual(values = c("dashed","solid")) + 
      scale_x_date(breaks=datebreaks1, labels=date_format("%Y")) + 
      ggtitle("Oil and Fianacial Market") + 
      annotate("rect", xmin=as.Date("2008-01-02"), xmax=as.Date("2008-06-30"), 
        ymin=-Inf, ymax=Inf, alpha=.1, fill="blue") + 
      theme(legend.position="bottom") + labs(linetype='') 

그런 다음 자산을 석유 및 석유로 변경하려고합니다.

내가 어떻게 할 수 있습니까?

scale_fill_discrete(labels=c("oil to asset" ,"asset to oil"))

가 작동하지 않습니다.

도와주세요.

+1

'linetype' 범례의 라벨을 바꾸려고하십니까? '채우기'척도가 아닌'scale_linetype_manual' 또는'scale_linetype_discrete'와 같은'linetype' 척도로 할 필요가 있습니다. – aosmith

+0

완전히 재현 할 수있는 예제를 첨부하면 도움이 될 때가 더 쉽습니다. 임의의 데이터 또는 실제 데이터에 대한 링크 포함. –

답변

0

이것은 실제로 @aosmith와 같은 해결책입니다. 오른쪽 breaks (labels 아님)을 scale_linetype_manual에 추가하십시오.

scale_linetype_manual(values = c("dashed","solid"), breaks=c("oil to asset" ,"asset to oil")) 

은 또한 당신이 spi$data1 <- factor(spi$data1)를 통해 spi$data1 요인을하는 것이 좋습니다. ggplot은 levels(some_factor_variable에서 요인 순서를 선택합니다. 요인 레벨 순서를 변경하여 모든 그래프에 breaks을 지정하지 않아도됩니다. Here이 그 예입니다.