4

Just wondering what is required so the colour for missing values is shown in the legend? Looking at example from the UseR! ggplot2 book, p94

p <- qplot(sleep_total, sleep_cycle, data=msleep, colour=vore)
p + scale_colour_hue(na.value = "Black")
p +  scale_colour_hue("What does \nit eat?", na.value="Black", breaks=c("herbi", "carni", "omni", "insecti", NA), labels=c("plants", "meat", "both", "insects", "don't know"))

the data point for vore=NA is shown in the plot but NA is not listed in the legend.

Thanks

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
user1420372
  • 2,077
  • 3
  • 25
  • 42

1 Answers1

4

Workaround for the problem would be to replace NA values in your data with same other character (for example, unknown) and plot data.

So, made new variable vore2 that has vore values as characters. Then replaced NA values with the unknown.

msleep$vore2<-as.character(msleep$vore)
msleep$vore2[is.na(msleep$vore2)]<-"unknown"

In the plot used new variable vore2 for the colors.

p <- qplot(sleep_total, sleep_cycle, data=msleep, colour=vore2)
p +  scale_colour_hue("What does \nit eat?", 
            breaks=c("herbi", "carni", "omni", "insecti", "unknown"), 
                labels=c("plants", "meat", "both", "insects", "don't know"))
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • Thanks for your suggestion - I had gotten the impression from the textbook that a GGPlot2 setting would make this work so that such a work-around was not required, but perhaps not! – user1420372 Jul 30 '13 at 02:49