问题
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
回答1:
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"))
来源:https://stackoverflow.com/questions/17848674/ggplot2-show-missing-value-colour-in-legend