2015-10-21 194 views
1

这是我的earlier question的后续行动。虽然krlmlr的答案有助于解决我的问题,但还有一个问题 - 标签远离点,以至于无法看到哪个标签对应于哪个点。有谁知道如何解决这一问题?标签点与ggplot2和直接标签

测试数据

test <- structure(list(ID = c(183, 184, 185, 186, 187, 188, 189, 190, 
    191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 202, 203, 204 
    ), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 10L, 1L, 1L, 11L, 1L, 10L, 10L, 1L, 1L, 1L), .Label = c("a", 
    "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", 
    "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"), class = "factor"), 
     x = c(27.4921834914348, 25.9627681619548, 30.4138361188149, 
     29.7795838507818, 32.33351964819, 31.9669266268744, 35.4433172141507, 
     37.8161067968601, 43.1590654001956, 44.2747819266045, 46.3829222044342, 
     42.2074195880057, 45.4532493462957, 48.393281430824, 51.7145681408198, 
     51.4911582677409, 51.9582538485293, 51.3328966791848, 36.4638478746633, 
     36.6113033420253, 39.3476493044906), y = c(-40.6667236544384, 
     -39.2640436250506, -40.6403794753022, -40.907139119954, -39.9691483441288, 
     -39.7485517513382, -38.595662907188, -38.2106224386729, -37.6418188651769, 
     -32.7096448721895, -34.1161006958616, -32.2821584775403, 
     -30.8436917254975, -30.3865899667262, -30.3910690518699, 
     -26.1013343566452, -23.8437232732877, -21.5548787351057, 
     5.50922747751602, 5.64434551903915, 5.01263995541617)), row.names = c(NA, 
    -21L), .Names = c("ID", "group", "x", "y"), class = "data.frame") 

library(ggplot2) 
library(directlabels) 

使用geom_text情节 - 问题:标签重叠

ggplot(test, aes(x=x, y=y)) + 
geom_point(aes(colour=group)) + 
geom_text(aes(label=ID), show_guide=F) 

enter image description here

情节的建议通过krlmlr - 问题:标签远离点

ggplot(test, aes(x=x, y=y)) + 
geom_point(aes(colour=group)) + 
geom_dl(aes(label = ID), method = defaultpf.ggplot("point",,,)) 

enter image description here

回答

3

一来避免重复方式(在一定程度上至少)将将每个标签抵消由其最近点确定的数量。因此,举例来说,如果一个点的最邻近的点是直接向它的右边,其标签会被放在左边,等

# centre and normalise variables 
test$yy <- (test$y - min(test$y))/(max(test$y) - min(test$y)) 
test$xx <- (test$x - min(test$x))/(max(test$x) - min(test$x)) 
test$angle <- NA 
for (i in 1:nrow(test)) { 
    dx <- test[-i, ]$xx - test[i, ]$xx 
    dy <- test[-i, ]$yy - test[i, ]$yy 
    j <- which.min(dx^2 + dy^2) 
    theta <- atan2((test[-i, ]$yy[j] - test[i, ]$yy), (test[-i, ]$xx[j] - test[i, ]$xx)) 
    test[i, ]$angle <- theta + pi 
} 
sc <- 0.5 
test$nudge.x <- cos(test$angle) * sc 
test$nudge.y <- sin(test$angle) * sc 

ggplot(test, aes(x=x, y=y)) + 
    geom_point(aes(colour=group)) + 
    geom_text(aes(x = x + nudge.x, y = y + nudge.y, label = ID), size = 3, show.legend = FALSE) 

enter image description here

你可以尝试用缩放参数sc玩弄(它越大,标签离点越远)以避免重叠标签。 (我想可能会发生不同的sc可以应用于所有点以避免重叠 - 在这种情况下,您需要更改每个点的缩放参数,可能通过使用dxdy定义sc)。

2

也许hjustvjust是你在找什么?

ggplot(test, aes(x=x, y=y)) + 
    geom_point(aes(colour=group)) + 
    geom_text(aes(label=ID), show_guide=F, hjust = 1.2, vjust = 0.5) 

enter image description here

+0

我的实际地块包括100-200分,这几乎是不可能找到hjust /一个值vjust避免任何重叠,因此我需要一个更“动态”的方式。 – beetroot

1

我想你可以通过调整参数抖动做到这一点 - ... position = position_jitter...。您可能要玩它一点点,因为你只给了我们数据的10%:

ggplot(test, aes(x=x, y=y)) + 
    geom_point(aes(colour=group), position= position_jitter(width= 1.5, height= 1)) + 
    geom_text(aes(label=ID), show_guide=F, hjust = 1.2, vjust = -.5, 
      position= position_jitter(width= 1.5, height= 1)) 

enter image description here

5

它可能是ggrepel更适合散点图中的点的标记。

library(ggplot2) # ggrepel requires ggpot2 v2.0.0 
library(ggrepel) 

ggplot(test, aes(x=x, y=y)) + 


geom_text_repel(aes(label = ID, color = group), show.legend = FALSE, 
        box.padding = unit(0.45, "lines")) + 

geom_point(aes(colour=group)) 

enter image description here