How to remove NA in the legend?

542 views
Skip to first unread message

zhan xw

unread,
Jul 23, 2018, 6:58:37 PM7/23/18
to ggtree
Hello,

Following ggtree documentation codes as following, I wonder how to hide "NA" from the figure legend(see attached).
Any suggestions? Thanks.

library(ggtree)
nwk
<- system.file("extdata", "sample.nwk", package="treeio")
tree
<- read.tree(nwk)
p
<- ggtree(tree)


dd
<- data.frame(taxa  = LETTERS[1:13],
                 place
= c(rep("GZ", 5), rep("HK", 3), rep("CZ", 4), NA),
                 value
= round(abs(rnorm(13, mean=70, sd=10)), digits=1))
## you don't need to order the data
## data was reshuffled just for demonstration
dd
<- dd[sample(1:13, 13), ]
row
.names(dd) <- NULL
print(dd)


p
<- p %<+% dd + geom_tiplab(aes(color=place)) +
  geom_tippoint
(aes(size=value, shape=place, color=place), alpha=0.25)
p
+ theme(legend.position="right")




How_to_remove_na_from_legend.jpg

Yu, Guangchuang

unread,
Jul 24, 2018, 12:26:53 AM7/24/18
to zhan xw, ggtree
use `scale_color_manual` to set the `breaks` that exclude `NA`.

--
G Yu, DK Smith, H Zhu, Y Guan, TTY Lam*. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data, Methods in Ecology and Evolution, 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
 
Homepage: https://guangchuangyu.github.io/software/ggtree
---
You received this message because you are subscribed to the Google Groups "ggtree" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree+unsubscribe@googlegroups.com.
To post to this group, send email to bioc-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bioc-ggtree/e51880ac-cad9-4119-8e81-9b7afd2f6310%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--~--~---------~--~----~------------~-------~--~----~
Guangchuang Yu PhD
Postdoc researcher
State Key Laboratory of Emerging Infectious Diseases
School of Public Health
The University of Hong Kong
Hong Kong SAR, China
-~----------~----~----~----~------~----~------~--~---

Xiaowei Zhan

unread,
Jul 24, 2018, 1:54:28 AM7/24/18
to gc...@connect.hku.hk, bioc-...@googlegroups.com
Your solution works now:

Use: 

p + theme(legend.position="right") + scale_color_manual(values = c("red", "blue", "green"), breaks=c("CZ", "GZ", "HK")) + scale_shape_manual(values = seq(3), breaks=c("CZ", "GZ", "HK"))

Instead of:
p + theme(legend.position="right")

Thanks. 

Best,
Xiaowei


To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree...@googlegroups.com.

To post to this group, send email to bioc-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bioc-ggtree/e51880ac-cad9-4119-8e81-9b7afd2f6310%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages