【问题标题】:Anchor text annotation in R plotR图中的锚文本注释
【发布时间】:2015-04-08 17:55:50
【问题描述】:

EDIT* 解决方案附在这个问题的底部

我有一个带有注释的图表,可以将客户端更改为客户端。我需要文本以最大值右对齐,以便任何客户端名称长度都不会覆盖图形的其他区域。这是代码和产品。只希望文本始终在箭头旁边结束,并向左增长。

x <- c(1,1,1,1,1,1,2,2,2,2,2,2)
y <- c(0,0,0,0,0,0,0,0.33,0.17,0.16,0.14,0.2)
z<-data.frame(cbind(x,y))
client.name = "x"
client.year = 2015

ggplot<-  
  ggplot(z,aes(x = x,y = y,fill = y))+
  geom_bar(stat = "identity", 
           fill = c("white","white","white","white","white","white",
                  "white","#c00000","#ed7d31","#ffc000","#92d050","#00b050"),width = .3)+
  theme(legend.position = "none") +
  theme(axis.ticks = element_blank()) +
  theme(panel.grid = element_blank()) +
  theme(axis.title = element_blank()) +
  theme(axis.text = element_blank()) +
# theme(panel.background = element_blank()) +
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))+
  geom_segment(aes(x = 1.77, y = .9, xend = 2.16, yend = .9),size=1.3,
               arrow = arrow(angle = 20, length = unit(0.25, "inches"), ends = "first", type = "open"),linetype="solid")+ 
  annotate("text", x = 2, y = c(0.167,0.415,0.58,0.73,0.9), 
           label = c("Disparaging","Unhappy","Ambivalent","Happy","Delighted"), 
           colour="white", fontface="bold", size=10) +
  annotate("text", x = 1, y =.9,  label = paste(client.name,client.year,paste0(.9, "%")), 
            fontface = "bold", hjust=0, size=10) 
ggplot

这是带有长客户端名称的结果,手动调整到箭头旁边

这是具有最小可能客户端名称“x”的结果。

我可以通过使用每个名称的值将文本放到我想要的位置,但需要自动调整到最右边的箭头,以获取任何给定的名称(加分,我什至可以换行吗很长的名字!?不知道怎么做)。

有什么想法吗?

解决方案: 正如@baptiste 所指出的, hjust 是这里的关键选项。我知道是这种情况,但不知道 hjust 超出 0:1 的值会根据字符串长度改变锚点的位置。您可以看到我使用 -1 来获得正确的定位,而我应该操作 X。我通过将 hjust 设置为 1(右对齐)并将 X 调整到所需位置来解决我的问题。

感谢收看。

【问题讨论】:

  • 请确保您的代码运行,现在有很多问题

标签: r ggplot2 annotations justify


【解决方案1】:

如果你想要右对齐文本,你应该使用hjust=1

lab = strwrap(paste(client.name,client.year,paste0(.9, "%")),10)
annotate("text", x = 1.77, y =.9,  label = paste(lab, collapse="\n"), 
         fontface = "bold", hjust=1, size=10) 

【讨论】:

  • 感谢您的回复,我已尝试实现 hjust 调用(参见代码),问题是文本的锚点随着字符串长度的变化而移动。我正在尝试设置右对齐,带有锚点,想法?另外,你能向我描述一下吗?我认为 hjust=1 应该将我的文本字符串设置在当前绘图区域的右侧,但实际上我必须设置大于 0:1 的值才能将字符串移动到我需要的位置,即让 x 自动设置起点右对齐的字符串。
  • hjust=1 将使文本右对齐,这意味着它延伸到 x 位置的左侧(此处为 1.77 以匹配箭头的尖端)。 [0,1] 之外的值总是令人困惑(通常最好避免),因为它们对应于字符串宽度 IIRC 的百分比。
  • 结合您上一条评论,您的建议解决了我的问题。我将更新我的帖子以显示答案,以及背后的推理,这对我的理解至关重要。
【解决方案2】:

有趣的问题,这只是部分解决方案。对于加分问题,请尝试这种方法。

  annotate("text", x = 1, y =.9,  label = paste("verylong\nclientname", client.year,paste0(.9, "%")), fontface = "bold", hjust=0, size=10) 

在算法上如何以及在何处插入换行符\n 更为复杂。您可能会说 10 个或更多字符是一个长客户端名称。然后,也许您可​​以在每个客户端名称上运行 nchar() 并按照以下伪代码的行设置测试:ifelse(nchar > 10, insert line break at position 11, leave away)

编辑关于客户姓名、日期和百分比的位置 现在你让我思考!

你为什么不用箭头上的当前端点来定义anchor &lt;- .9#?

然后您将您的客户名称等与x = anchor - .1 或一些水平调整放在左边?在您当前的 MWE 数据中,x 主要是 1 和 2,它可能不太有效,但是对于真实数据,我认为这可能会将注释贴在箭头的末端。

【讨论】:

  • 所以如果你有兴趣,我最终设置了我的规则来分解字符串数组,如下所示:if (nchar(client.name) &gt; 40) {warning("Client name is too long")} if (nchar(client.name) &gt; 20) { space.locs = as.data.frame(str_locate_all(client.name, " ")) #locate spaces in name space.mid = round(nrow(as.data.frame(space.locs))/2)# find middle space substr(client.name,space.locs[space.mid,1],space.locs[space.mid,1]) &lt;- "\n" #add line break }
猜你喜欢
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多