Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

r - Consistent color scale and legend between plots when not all levels of a grouping variable are present in the data

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Set the factor levels of Colours to include all possible values, whether present or not in the data at hand, then add drop=FALSE to scale_colour_manual:

all.data=data.frame(Date=dates15, Data=ex.data, Colours=colours)
g.cols=c("black", "blue", "pink", "purple")
all.data$Colours = factor(all.data$Colours, levels=sort(c(unique(colours), "Pink Points")))

ggplot(all.data, aes(Date, Data, colour=Colours, group=1)) + 
  geom_line() +
  scale_color_manual(values=g.cols, drop=FALSE) +
  xlim(as.POSIXct("2015-01-01 00:00:00"), as.POSIXct("2015-02-12 23:45:00"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...