简体   繁体   English

将文本放置在距图表固定的距离处

[英]Place text at a fixed distance from chart

Please help me find a universal way to place text at a constant distance from the right side of the plot area as illustrated below. 请帮助我找到一种通用方法,将文本放置在距离绘图区域右侧恒定的距离处,如下图所示。 Since the plot area is narrower on the right, unfortunately, the current placement calculation positions the text too far to the left. 由于情节区域在右侧较窄,不幸的是,当前的放置计算将文本定位得太靠左。 Note the text is right-aligned. 请注意,文本是右对齐的。

My last idea: If we knew the distance in centimeters between say x=1 and x=2, we could calculate the position easily. 我的最后一个想法:如果我们知道在x = 1和x = 2之间的厘米距离,我们可以很容易地计算出位置。 Unfortunately, it's apparently not that easy to get that distance. 不幸的是,距离显然并不那么容易

FYI: I am not looking to label the lines. 仅供参考:我不打算标注这些线条。

在此输入图像描述 Full-size image 全尺寸图片

library(ggplot2)
library(reshape)
library(gridExtra)

df = data.frame(x =(1:3),One=c(12, 8, 13),Two=c(13, 7, 11),Three=c(11, 9, 11))
df.melt = melt(df, id.vars="x")
xmax = max(df.melt$x); xmin = min(df.melt$x)
ymax = max(df.melt$value); ymin = min(df.melt$value)

dfa = data.frame(x=(xmax-xmin)*1.15+xmin, y=c(11,12,13.5), ann=c("10.1|","1.1|","Texttexttext|"))
dfa.melt = melt(dfa, id.vars=c("x","ann"))

p = ggplot() + 
  geom_line(data=df.melt,  aes(x=x, y=value, color=variable), show_guide=F) +
  geom_text(data=dfa.melt, aes(x=x, y=value, hjust = 1, label=ann), size=3) + 
  coord_cartesian(xlim=c(xmin,xmax), ylim=c(ymin,ymax))

p1 = p + theme(plot.margin=unit(c(1,3,0,0),"cm"), axis.text.y=element_text(size=10))
p2 = p + theme(plot.margin=unit(c(1,3,0,3),"cm"), axis.text.y=element_text(size=35))
p1c <- ggplot_gtable(ggplot_build(p1))
p1c$layout$clip[p1c$layout$name=="panel"] <- "off"
p2c <- ggplot_gtable(ggplot_build(p2))
p2c$layout$clip[p2c$layout$name=="panel"] <- "off"
grid.arrange(p1c, p2c, ncol=2)

Because everything is named, it is possible to access any component of the plot using the grid functions. 因为所有内容都已命名,所以可以使用网格函数访问绘图的任何组件。

The problem is ggplot2 creates many viewports and grobs when it draws a plot. 问题是ggplot2在绘制绘图时创建了许多视口和凹凸。 So it is is not easy to make changes to this plot. 所以改变这个情节并不容易。 I notice also that the name of grobs and viewports changes for each new execution of the plot. 我还注意到,每次执行绘图时,grobs和视口的名称都会发生变化。 So I tried to get viewports by some criteria (here depth = 4) 所以我尝试按一些标准获取视口(这里深度= 4)

#Get the viewports: 
scene.vps <- grid.ls(viewports=T,grobs=F)
scene.flat <- as.data.frame(do.call('cbind',scene.vps))
vps <- subset(scene.flat ,vpDepth == '4')$name[1:2]
vps <- as.character(vps)


# modify the plot by grid.text
depth <- downViewport(vps[1])
grid.text("1.1|",x=unit(1, "npc") - unit(1, "mm"),
                 y=unit(1, "npc") - unit(20, "mm"),
                 just=c("right", "top"))

grid.text("10.1|",x=unit(1, "npc") - unit(1, "mm"),
                  y=unit(1, "npc") - unit(60, "mm"),
                  just=c("right", "top"))

grid.text("Texttexttext|",x=unit(1, "npc") - unit(10, "mm"),
                          y=unit(1, "npc") -unit(2,'mm'),
          just=c("right", "top"))

upViewport(depth)  # come back to the root viewport

depth <- downViewport(vps[2])

grid.text(...

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM