I have data which is being sequentially added to a data.frame
in R. I am creating plots every so often showing the results. The plot is colour coded according to certain criteria, some of which are never met, hence there is not this colour on the diagram.
For example,
library(ggplot2)
dates15=seq(as.POSIXct("2015-01-01 00:00:00"), as.POSIXct("2015-06-30 23:45:00"), by="15 min")
ex.data=rnorm(length(dates15),2,1)
blue=c(1:5000)
pink=which(ex.data>50)
purple=c(10000:15000)
colours=rep("Black points", length(dates15))
colours[blue]="Blue Points"
colours[pink]="Pink points"
colours[purple]="Purple points"
all.data=data.frame(Date=dates15, Data=ex.data, Colours=colours)
g.cols=c("black", "blue", "pink", "purple")
ggplot(all.data, aes(Date, Data, colour=Colours, group=1))+geom_line()+scale_color_manual(values=g.cols)+
xlim(as.POSIXct("2015-01-01 00:00:00"), as.POSIXct("2015-02-12 23:45:00"))
In this example, I've set the variable pink to be points which are only greater than 50 (which is clearly not possible in my data). So when the plot is created, the 'Pink
' legend name is missing, but the colour pink has been assigned to the purple label. I would like the colours and labels to stay matched all the time, even if there is a variable which isn't used.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…