2

我正在寻找一种避免散点图中标签重叠的方法。我发现 directlabels 工作正常,但不允许我使用表达式作为标签,也不允许选择要显示的标签和不显示的标签。

这是一个例子:

p1 使用 geom_text() 绘制一些(我选择的)标签,表达式解析允许希腊字母和上标之类的东西,但标签可以重叠。

p2 避免与 directlabels() 重叠,但我无法控制绘制哪些标签,也无法使用表达式解析。

我怎样才能同时拥有这两种方法的优势?

require(ggplot2)
require(directlabels)

par <- c("Ei", "Gb", "Gf", "Gl", "w1", "w2", "w3", "w4",
     "w5", "Alpha", "Beta", "Amin", "dl", "df")
df <- data.frame(x=runif(14), y=runif(14), Parameters=par)

# labels with expressions
labels <- c("Ei" = 'E[i]', "Gb" = 'G[b]', "Gf" = 'G[f]', "Gl" = 'G[l]', 
             "w1" = 'omega[1]', "w2" = 'omega[2]', "w3" = 'omega[3]', "w4" = 'omega[4]', 
             "w5" = 'omega[5]', "Alpha" = 'alpha', "Beta" = 'beta', "Amin" = 'A[min]', 
             "dl" = 'd[l]', "df" = 'd[f]')

# and missing values
labels[which(sqrt(df$x^2 + df$y^2) < .5)] <- NA


p1 <- ggplot(data=df, aes(y=y, x=x, colour=Parameters)) + 
  geom_point() + scale_colour_discrete() +
  coord_cartesian(xlim=c(-2,3), ylim=c(-2,3)) + # causes overlapping
  geom_text(aes(label = labels), hjust=-0.3, vjust=1, 
            show_guide=FALSE, parse=TRUE) 

p1 # selected labels appear with correct expression


p2 <- direct.label(p1, method='smart.grid')
p2 # all labels appear (not wanted) without expression parsing
4

0 回答 0