2014-01-08 94 views
5

我在避免圖形中重疊標籤時遇到問題。我看了一些類似的問題,但沒有一個幫助我解決我的問題。下面我提供一個可重複的例子。另外,我不明白爲什麼direct.labels沒有爲每個標籤選擇適當的顏色。避免標籤與直接標籤和ggplot2重疊

mydf <- structure(list(ano = c(1970, 1975, 1980, 1985, 1990, 1995, 2000, 
2004, 2005, 2006, 2007, 2008, 2009, 1970, 1975, 1980, 1985, 1990, 
1995, 2000, 2004, 2005, 2006, 2007, 2008, 2009, 1970, 1975, 1980, 
1985, 1990, 1995, 2000, 2004, 2005, 2006, 2007, 2008, 2009, 1970, 
1975, 1980, 1985, 1990, 1995, 2000, 2004, 2005, 2006, 2007, 2008, 
2009, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2004, 2005, 2006, 
2007, 2008, 2009, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2004, 
2005, 2006, 2007, 2008, 2009, 1970, 1975, 1980, 1985, 1990, 1995, 
2000, 2004, 2005, 2006, 2007, 2008, 2009), field = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("Humanites", 
"Scial Sciences", "Natural Sciences", "Computer science and engineering", 
"education", "business", "Other fields"), class = "factor"), 
value = c(143549, 150736, 134139, 132891, 172485, 193404, 
214107, 254847, 261696, 265808, 274535, 278387, 280993, 193511, 
176674, 141581, 134468, 183762, 199895, 201681, 242506, 249619, 
254222, 259950, 262771, 269996, 81916, 91596, 78092, 76228, 
70209, 93443, 89772, 99370, 105899, 113077, 117200, 121009, 
125809, 52570, 52328, 90476, 139459, 104910, 102503, 117011, 
133655, 128886, 124024, 122084, 122408, 128318, 176307, 154437, 
108074, 87147, 110807, 105384, 105458, 105451, 107238, 105641, 
102582, 101708, 101265, 115396, 143171, 200521, 236700, 249165, 
226623, 263515, 311574, 318042, 327531, 335254, 347985, 358293, 
76481, 156804, 182257, 180930, 203200, 243540, 252627, 291861, 
313862, 333789, 351464, 367100, 385340)), .Names = c("ano", 
"field", "value"), row.names = c(NA, -91L), class = "data.frame") 

require(ggplot2) 
require(directlabels) 
require(scales) 

p <- ggplot(mydf, aes(y=value, x=ano, group=field)) + geom_line(aes(group=field,  colour=field), show_guide = FALSE) + 
ylab("Number of B.A. degrees awarded") + xlab("year") + theme_hyper() + 
theme(legend.key = element_rect(colour = "white")) + scale_y_continuous(labels=comma) 

p1 <- p + geom_dl(aes(label=field), list('last.points', cex = 1.3, hjust = 1)) 
p1 

功能theme_hyper情況如下:

theme_hyper <- function(angle=0, fonte="arial", size=14) { 
theme(panel.grid.major = element_blank()) + 
theme(panel.background = element_blank())+ 
theme (panel.grid.minor = element_blank()) + 
theme(legend.title = element_text(family=fonte, size = size, face = "bold")) + 
theme(legend.text = element_text(family=fonte, size = size, face = "bold")) + 
theme(plot.title = element_text(family=fonte, size = size, face = "bold")) + 
theme(axis.text.x = element_text(family=fonte, size = size, face = "bold", angle = angle, vjust = .5)) + 
theme(axis.text.y= element_text(family=fonte, size = size, face = "bold", hjust=.5)) + 
theme(axis.title.x= element_text(family=fonte, size = size, face = "bold")) + 
theme(axis.title.y= element_text(family=fonte, size = size, face = "bold")) 

} 

回答

6

使用"last.bumpup"

添加colour=field增添色彩給你的標籤。

geom_dl(aes(label=field, colour=field), list('last.bumpup', cex = 1.3, hjust = 1)) 

刪除彩色圖例/指南。

scale_colour_discrete(guide="none") 

最終調用:

p <- ggplot(mydf, aes(y=value, x=ano, group=field)) + 
    geom_line(aes(group=field, colour=field), show_guide = FALSE) + 
    ylab("Number of B.A. degrees awarded") + xlab("year") + theme_hyper() + 
    theme(legend.key = element_rect(colour = "white")) + 
    scale_y_continuous(labels=comma) + 
    geom_dl(aes(label=field, colour=field), list('last.bumpup', cex = 1.3, hjust = 1)) + 
    scale_colour_discrete(guide="none") 
+0

非常感謝你。 –

2

也許嘗試

direct.label(p, list("last.points", cex=.7, hjust=1)) 

或的許多其他的可能性here之一。

2

您是否考慮嘗試使用ggrepel來放置沒有重疊的標籤?

library(ggrepel) 
library(scales) 

ggplot(mydf, aes(y=value, x=ano, group=field)) + 
    geom_line(aes(group = field, color = field), show.legend = FALSE) + 
    ylab("Number of B.A. degrees awarded") + 
    xlab("year") + 
    coord_cartesian(xlim = c(min(mydf$ano), max(mydf$ano) + 3)) + 
    geom_text_repel(
    data = subset(mydf, ano == max(ano)), 
    aes(label = comma(value), color = field), 
    size = 6, 
    nudge_x = 3, 
    segment.color = NA, 
    show.legend = FALSE 
) 

enter image description here