Skip to content

Commit

Permalink
add ggrepel functionality for labels
Browse files Browse the repository at this point in the history
  • Loading branch information
wabarr committed Dec 5, 2017
1 parent 385b321 commit 4522e78
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 26 deletions.
11 changes: 9 additions & 2 deletions R/ggphylomorpho.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @param title the text string to use as the title of the phylomorphospace plot
#' @param xlab the text label for the x axis
#' @param ylab the text label for the y axis
#' @param repel boolean indicating whether or not to use the \code{ggrepel} package to prevent overplotting of labels
#' @return the ggplot object representing the phylomorphospace


Expand All @@ -20,11 +21,13 @@ ggphylomorpho <- function(tree,
labelvar=taxon,
title="Phylomorphospace",
xlab="PC1",
ylab="PC2")
ylab="PC2",
repel=TRUE)
{

require(ggplot2)
require(phytools)
require(ggrepel)

## create matrix for use in phytools::fastAnc()
mat <- cbind(eval(substitute(xvar), tipinfo),eval(substitute(yvar), tipinfo))
Expand Down Expand Up @@ -61,9 +64,13 @@ ggphylomorpho <- function(tree,
ggplot() +
geom_segment(data=edgecoords,aes(x=x.x,xend=x.y, y=y.x, yend=y.y)) +
geom_point(data=pointsForPlot, aes(x=x, y=y, color=color), size=5) +
geom_text(data=pointsForPlot, aes(x=x, y=y, label=label)) +
labs(title=title, x=xlab, y=ylab) +
theme_bw(20) +
theme(legend.position='bottom')
if(repel){
theplot <- theplot + geom_text_repel(data=pointsForPlot, aes(x=x, y=y, label=label))
} else{
theplot <- theplot + geom_text(data=pointsForPlot, aes(x=x, y=y, label=label))
}
return(theplot)
}
5 changes: 3 additions & 2 deletions R/makesampledata.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
makesampledata <- function(){
require(phytools)
tree<-pbtree(n=26,tip.label=LETTERS)
labs <- sapply(LETTERS, FUN=function(x) paste(rep(x, 5), collapse=""))
tree<-pbtree(n=26,tip.label=labs)
DF <- data.frame(
PC1=fastBM(tree),
PC2=fastBM(tree),
taxon=LETTERS,
taxon=labs,
group=rep(c("group1","group2"),13)
)
return(list(tree=tree, DF=DF))
Expand Down
8 changes: 5 additions & 3 deletions man/ggphylomorpho.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions readme.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ head(sampleData$DF)
```

```
## PC1 PC2 taxon group
## A -2.97831839 2.67580286 A group1
## B -0.18117859 -1.14645602 B group2
## C -0.69258460 -2.32020109 C group1
## D -0.40664199 0.09467764 D group2
## E -0.01512143 1.23078740 E group1
## F 0.93301701 0.65321188 F group2
## PC1 PC2 taxon group
## AAAAA 1.416156 -3.4994342 AAAAA group1
## BBBBB 2.364885 -3.1435084 BBBBB group2
## CCCCC 3.923850 -3.7085053 CCCCC group1
## DDDDD 4.779961 -3.1848266 DDDDD group2
## EEEEE 1.433133 2.9528747 EEEEE group1
## FFFFF -0.273229 -0.6153418 FFFFF group2
```

## check out the documentation for the function
Expand Down
Binary file modified readme_files/figure-html/unnamed-chunk-1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified readme_files/figure-html/unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4522e78

Please sign in to comment.