r - How to add labels to ggplot segment? -


i'm trying add labels segments created in graph using ggplot

dat <- data.frame(start <- c(-1.05113647, -0.63911585, -0.62791554),                   end <- c(0.37491159, -0.13911585, -0.12791554),                    order <- c("sei whale", "probeagle", "northern fur seal"),                   pos <- c(1, 2, 3))   ggplot(dat) +    geom_segment(aes(x=start, y=start, xend=end, yend=start), colour = "blue", size = 2) +   scale_y_reverse() +   xlab("pc1")+   ylab(" ")+   theme_linedraw() +    theme(panel.grid.minor = element_blank(), panel.grid.major =   element_blank()) +    theme(aspect.ratio = 0.3) +   theme(legend.position="none") +   theme(axis.ticks = element_blank(), axis.text.y = element_blank())  

i'd know how add names "order" respective segments.

you try geom_dl() directlabels package , expand x axis bit:

library(ggplot2) library(directlabels)  ggplot(dat) +    geom_segment(aes(x = start, y = start, xend = end, yend = start),                 colour = "blue", size = 2) +   scale_y_reverse() +   geom_dl(aes(end, start, label = order),            method = list(dl.trans(x = x + 0.2), "last.bumpup", cex = 0.60)) +   scale_x_continuous(expand = c(0, 0.2)) +   labs(x = "pc1", y = " ") +   theme_linedraw() +    theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +    theme(aspect.ratio = 0.7) +   theme(legend.position = "none") +   theme(axis.ticks = element_blank(), axis.text.y = element_blank())  

which gives:

enter image description here

note: changed aspect.ratio 0.7 make more readable.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -