Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 549
sym246 Avatar asked Jan 22 '16 15:01

sym246


People also ask

How do I specify colors in ggplot2?

When creating graphs with the ggplot2 R package, colors can be specified either by name (e.g.: “red”) or by hexadecimal code (e.g. : “#FF1234”). It is also possible to use pre-made color palettes available in different R packages, such as: viridis, RColorBrewer and ggsci packages.

Which ggplot2 function can create a complete plot given the data mappings and geom as parameters?

Anything you put in the ggplot() function can be seen by any geom layers that you add (i.e., these are universal plot settings). This includes the x- and y-axis you set up in aes() . You can also specify aesthetics for a given geom independently of the aesthetics defined globally in the ggplot() function.

How do you set a color in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.


1 Answers

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"))
like image 151
eipi10 Avatar answered Sep 27 '22 22:09

eipi10