[英]ggplot2 change labels in a legend
I want to change Labels in a Legend. 我想更改图例中的标签。
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='')
Then I want to changeing asset to oil and oil to asset. 然后,我想将资产更改为石油,并将石油更改为资产。
How I can chahge? 我该如何挑战?
scale_fill_discrete(labels=c("oil to asset" ,"asset to oil"))
This is not working. 这是行不通的。
Please help me. 请帮我。
This is actually same solution as @aosmith suggested - add right breaks
(not labels
) to your scale_linetype_manual
. 这实际上是相同的溶液@aosmith提示-右加breaks
(没有labels
)到您的scale_linetype_manual
。
scale_linetype_manual(values = c("dashed","solid"), breaks=c("oil to asset" ,"asset to oil"))
I also recommend you to make your spi$data1
a factor via spi$data1 <- factor(spi$data1)
. 我还建议您通过spi$data1 <- factor(spi$data1)
使spi$data1
成为一个因子。 ggplot chooses factor ordering from levels(some_factor_variable
. You may avoid specifying the breaks
on every graph by changing factor levels ordering. Here is an example how to do it. ggplot从levels(some_factor_variable
选择因子排序levels(some_factor_variable
。您可以通过更改因子级别排序来避免在每个图形上指定breaks
。 这是一个示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.