保持R中标签的下划线长度与标签长度一致

时间:2014-04-17 03:49:46

标签: r ggplot2

我正在创建两个顶点之间的最短路径中涉及的所有节点和边的可视化。在没有给出不必要的细节的情况下,节点代表有机体,边缘代表它们之间的父子关系。节点的x轴表示其 birthyear ,而y轴只是路径中节点的索引(相邻节点之间用单位值分隔)。

我将通过以下培训数据集显示我的问题:

xstart=c(1966, 1967,1977,1975,1958)
ystart=c(1.1,2.1,3.1,4.1,5.1)
xend=c(1967,1977,1975,1958,1958)
yend=c(1.9,2.9,3.9,4.9,5.1)
x=c(1966,1967,1977,1975,1958)
y=c(1,2,3,4,5)
xustart=c(1964,1965,1975,1973,1956)
xuend=c(1968,1969,1979,1977,1960)
yustart=c(0.9,1.9,2.9,3.9,4.9)
yuend=c(0.9,1.9,2.9,3.9,4.9)
xostart=c(1964,1965,1975,1973,1956)
xoend=c(1968,1969,1979,1977,1960)
yostart=c(1.1,2.1,3.1,4.1,5.1)
yoend=c(1.1,2.1,3.1,4.1,5.1)
label=c("node1","node2","node3","node4","node5")
path.plot <- data.frame(label,xstart,ystart,xend,yend,x,y)

如果我只创建节点和边缘(边缘进入和离开节点位于文本标签的中心x值),我发现视觉效果看起来没有吸引力:

ggplot(data=path.plot) +
  geom_segment(aes(x=xstart, y=ystart, xend=xend, yend=yend)) + 
  geom_text(aes(x=x, y=y, label=label))

我认为下划线看起来好一点,但仍然草率:

ggplot(data=path.plot) +
  geom_segment(aes(x=xstart, y=ystart, xend=xend, yend=yend)) +
  geom_segment(aes(x=xustart, y=yustart, xend = xuend, yend = yuend)) +
  geom_text(aes(x=x, y=y, label=label))

老实说,我认为边缘连接到左下角并且写下文本标签的下划线看起来最好(例如,传入边缘连接到下划线的左侧点,传出边连接到下划线的右边点。)

然而,这不会像node1和node2那样起作用,其中离开node1的边将跨越一些文本,并最终作为负斜率边缘,这可能是欺骗,因为node2稍后date(1967),而不是node1(1966)。

所以,然后我尝试了下划线和上划线:

ggplot(data=path.plot) +
  geom_segment(aes(x=xstart, y=ystart, xend=xend, yend=yend)) + 
  geom_segment(aes(x=xustart, y=yustart, xend = xuend, yend = yuend)) +
  geom_segment(aes(x=xostart, y=yostart, xend = xoend, yend = yoend)) +
  geom_text(aes(x=x, y=y, label=label))

enter image description here

我认为这看起来最好,但如果你有任何改进建议,我会考虑到这一点。无论如何,我的主要问题是如何创建下划线/上划线的宽度与节点标签的宽度成比例?因为我的节点标题不会总是相同的长度(例如我只使用了通用标题)。它们的标签长度​​可能大不相同,因此具有一致长度的下划线不起作用。

当前下划线/上划线的另一个问题是,如果我重新调整图像大小,文本标签长度将与下划线/上划线长度成比例变化,突然看起来很荒谬!所以,我想我也试图找到一种方法将下划线/上划线的长度连接到文本的长度,这样它们不仅保证成比例,而且保证在图像大小为时保持成比例重新调整。

谢谢...


我考虑了strwidth选项和矩形(来自第一条评论) - 它看起来很吸引人。但是,当我调整大小时仍然存在问题(标签长度和矩形长度在调整图像大小时不会保持比例)。例如,如果我在R studio的绘图窗口中使图像稍微小一些,则标签会在矩形上流失。我没有使用静态图像,因此我不能简单地将图像保存为正确的大小。

以下是我作为更新所做的事情,但仍然想知道即使整个图像调整大小,我是否可以保持标签/矩形长度比例:

# Made one label longer than the others
label=c("node1","node238968396849223","node3","node4","node5") 

textFrame = data.frame(x = x, y = y, label = label)
textFrame = transform(textFrame,
                      w = strwidth(label, 'inches') + 0.25,
                      h = strheight(label, 'inches') + 0.25
)

ggplot(data = path.plot,aes(x = x, y = y)) + 
  geom_rect(data = textFrame, aes(xmin = x - strwidth(label, "inches")*1.2, xmax = x + strwidth(label, "inches")*1.2, 
                                  ymin = y-.1, ymax = y+.1), fill = "grey80") +
  geom_segment(aes(x=xstart, y=ystart, xend=xend, yend=yend)) +
  geom_text(data = textFrame,aes(x = x, y = y, label = label), size = 4)

1 个答案:

答案 0 :(得分:1)

您可以使用strwidth。例如

geom_segment(aes(x = x-strwidth(label, "inches")*1.2, y=yustart, 
                 xend = x+strwidth(label, "inches")*1.2, yend = yuend))

另请参阅this post,如果您想制作一个方框(color =“black”,fill =“transparent”)而不是行。