2

默认情况下,包“directlabels”显然会尝试通过减小标签字体大小来使每个标签适合正常绘图区域内。

下面,Label for OneLabel for Two应该Label for Threeeeeeeeee都具有相同的字体大小,如果标签超出绘图区域clip是可以的(因为禁用自定义注释以显示)。

事实上,我可能想要设置xlim=c(1,3)并且标签应该完全在绘图区域之外。下图左栏为xlim=c(1,3.4),右栏为xlim=c(1,3)

我了解到cex可以用来重置标签的字体大小,但它似乎与包的标签分离算法冲突。下图的顶行是否cex,底行是cex=1

找到一种方法让标签不重叠,字体大小相同,并且与xlim=c(1,3)and一起使用会很棒xlim=c(1,3.4)

在此处输入图像描述

library(reshape)
library(ggplot2)
library(directlabels)

df=data.frame(
  x = 1:3,
  One=c(12, 8, 13),
  Two=c(13, 7, 11),
  Threeeeeeeeee=c(11, 9, 11))

df.d.melt = melt(df[,c("x","One","Two","Threeeeeeeeee")], id.vars="x")
df.d.melt$variable1 = df.d.melt$variable
levels(df.d.melt$variable1) = paste("","Lable for",levels(df.d.melt$variable1))

p = ggplot(df.d.melt, aes(x=x, y=value, color=variable)) + 
  geom_line(size=1.1) +
  geom_text(aes(x =3.4, y=8, label="Custom Outside\nChart Annotation"), show_guide=FALSE) + 
  coord_cartesian(xlim=c(1,3.4)) +
  geom_dl(aes(label=variable1), method=list("last.qp", cex=1), show_guide=FALSE) + 
  theme(legend.position="top",plot.margin = unit(c(0, 4, 0, 0), "cm")) 

p1 <- ggplot_gtable(ggplot_build(p))
p1$layout$clip[p1$layout$name=="panel"] <- "off"
grid.draw(p1)
4

2 回答 2

1

默认情况下,directlabels 假定您希望在绘图区域内具有可读标签,因此它会减小文本大小,以便使用reduce.cex.lr将标签放入内部。可以通过定义不减小文本大小的自定义定位方法来完成绘图之外的标签,例如

do.not.reduce <-
  list(cex=2, "last.points", "calc.boxes",
       qp.labels("y", "bottom", "top", make.tiebreaker("x", "y")))

WithLegend <- ggplot(df.d.melt, aes(x=x, y=value, color=variable)) + 
  geom_line(size=1.1) +
  geom_text(aes(x =3.4, y=8, label="Custom Outside\nChart Annotation")) + 
  coord_cartesian(xlim=c(1,3)) +
  theme(plot.margin = unit(c(0, 4, 0, 0), "cm"))
WithLabels <- direct.label(WithLegend, "do.not.reduce")
GTable <- ggplot_gtable(ggplot_build(WithLabels))
GTable$layout$clip[GTable$layout$name=="panel"] <- "off"
grid.draw(GTable)
于 2013-11-13T02:04:31.400 回答
0

一个可行的解决方案是将标签附加到图表左侧的附加不可见短线(例如定义x=1x=1.001),并使用 directlabels'trans_new将注释移动到所需的 x 值。

于 2012-12-14T16:09:17.870 回答