NA value in the legend of the plot ?

2,002 views
Skip to first unread message

Michel ARNAUD

unread,
Mar 24, 2014, 4:24:16 AM3/24/14
to ggp...@googlegroups.com
Hi
I have the following problem :
With these instructions, why NA is it not shown in the legend of the plot ?

qplot(sleep_total, sleep_cycle, data=msleep, colour=vore) + 
scale_colour_hue(
"que mange\nt il ?", 
breaks = c("herbi", "carni", "omni", "insecti", NA),
labels = c("plante", "viande", "tout", "insecte", "Ne sais pas"))
Thanks for your help
Michel





Brandon Hurr

unread,
Mar 24, 2014, 4:39:13 AM3/24/14
to Michel ARNAUD, ggplot2
If you put NA in "" does it put it in? 

breaks = c("herbi", "carni", "omni", "insecti", "NA"),

B


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michel ARNAUD

unread,
Mar 24, 2014, 5:26:55 AM3/24/14
to Brandon Hurr, ggplot2

same result !!!

Michel

 


Michel Arnaud
5 rue du Cannau
34000 Montpellier

Brandon Hurr

unread,
Mar 24, 2014, 5:33:02 AM3/24/14
to Michel ARNAUD, ggplot2
I didn't realize that msleep was a dataset in R so I didn't try it, but now that I did I can see the problem. 

Any place where there is NA in the "vore" column, there is missing data in sleep_total or sleep_cycle. When I run the code I get... 
Warning message:
Removed 51 rows containing missing values (geom_point). 

The rows that are being removed from this dataset include the rows that are NA in "vore", because they are NA in one or both of the other columns that you are using geom_point to plot. 

Make sense? 

B

Michel ARNAUD

unread,
Mar 24, 2014, 5:59:34 AM3/24/14
to Brandon Hurr, ggplot2
No because

msleep[!is.na(msleep$sleep_total) & !is.na(msleep$ sleep_cycle) ,c(3,6,8)]
      vore sleep_total sleep_cycle
4     omni        14.9   0.1333333
5    herbi         4.0   0.6666667
6    herbi        14.4   0.7666667
7    carni         8.7   0.3833333
9    carni        10.1   0.3333333
12   herbi         9.4   0.2166667
14   herbi        12.5   0.1166667
17    omni         9.1   0.1500000
18   carni        17.4   0.3833333
20    omni        18.0   0.3333333
22 insecti        19.7   0.1166667
23   herbi         2.9   1.0000000
25    omni        10.1   0.2833333
28   carni        12.5   0.4166667
29    omni         9.8   0.5500000
34    omni         8.0   1.5000000
38    omni        10.1   0.7500000
40   herbi        14.3   0.2000000
42   herbi        12.5   0.1833333
43 insecti        19.9   0.2000000
48   herbi         8.4   0.4166667
50    omni         9.7   1.4166667
54    omni         9.4   0.6666667
64   herbi        13.0   0.1833333
67 insecti         8.4   0.1666667
68   herbi        11.3   0.1500000
71   herbi        13.8   0.2166667
73    <NA>        12.8   0.1833333
74    omni         9.1   0.5000000
77   herbi         4.4   0.9000000
79    omni         8.9   0.2333333
83   carni         9.8   0.3500000


Michel Arnaud
5 rue du Cannau
34000 Montpellier


Brandon Hurr

unread,
Mar 24, 2014, 6:26:11 AM3/24/14
to Michel ARNAUD, ggplot2
Michel, 

I missed that one. It took me a while to realize that it was actually plotting that point in grey, but not putting anything into the legend. 

Best I can do is to convert it to a character and then give it a non-NA name. 

msleep$vore<-as.character(msleep$vore)
msleep[which(is.na(msleep$vore)),]$vore<-"None"

ggplot(data=msleep)+
geom_point(aes(sleep_total, sleep_cycle, colour=vore), size=3)


Michel ARNAUD

unread,
Mar 24, 2014, 8:42:11 AM3/24/14
to Brandon Hurr, ggplot2
Merci beaucoup  Brandon
Bonne journée à toi

Michel Arnaud
5 rue du Cannau
34000 Montpellier


Dennis Murphy

unread,
Mar 24, 2014, 5:46:20 PM3/24/14
to Brandon Hurr, Michel ARNAUD, ggplot2
There is an argument in the scale functions to plot NA values of the
aesthetic in a different color (the default is "gray50" or "grey50",
depending on your preferred spelling); however, it does not print an
entry in the legend because by default, NAs are excluded as legitimate
factor levels. If you use the 'exclude = NULL' argument in factor() to
generate an NA level, it still doesn't get passed to the legend in
ggplot2. Therefore, you need to resort to the 'replace the NA by
something else' in order to have it represented in the legend.

I went about this in a more roundabout way, but got to the same place:

msleep2 <- msleep[!is.na(msleep$sleep_cycle), ]

# Recast vore as a factor with an NA level
msleep2$Vore <- as.character(msleep2$vore)
msleep2$Vore[is.na(msleep2$Vore)] <- "NA"
msleep2$Vore <- factor(msleep2$Vore, levels = c("carni", "herbi", "insecti",
"omni", "NA"))

qplot(sleep_total, sleep_cycle, data=msleep2, colour=Vore) +
scale_colour_manual("que mange\nt il ?",
values = c("firebrick", "brown", "darkorange", "dodgerblue", "black"),
labels = c("plante", "viande", "tout", "insecte", "Ne sais pas"))

Dennis

Michel ARNAUD

unread,
Mar 27, 2014, 10:50:19 AM3/27/14
to ggp...@googlegroups.com, Brandon Hurr, Michel ARNAUD
Merci Denis
Michel
Reply all
Reply to author
Forward
0 new messages