相对于图例中的文本,符号的垂直调整

huangapple go评论54阅读模式
英文:

Vertical adjustment of symbols relative to text in legend

问题

关于您的问题,您希望将图例中的符号与相应文本的第一行垂直对齐。这可以通过使用\n来实现。您可以将long\ntext更改为long,以确保符号与标签的第一行对齐。

plot(table(iris$species))
legend('right', c('A', 'B', 'long'), fill = colors(3))

这样,符号将与标签的第一行(即long)垂直对齐。

英文:

Is there a way to vertical align the symbols in a legend relative to the first line of the corresponding text?

plot(table(iris$species))
legend('right',c('A','B','long\ntext'), fill = colors(3))

The labels A and B are vertically aligned to the corresponding symbols, but the symbol corresponding to the the third label (long text with a line-break between the words) is aligned to the middle of this label.

I would prefer if the symbol would be aligned to the first line of the label (i.e. long).

答案1

得分: 2

以下是已翻译的代码部分,不包括问题的答案:

在文本之前放一个空行是最简单的方法。这样,文本的第一行将与框对齐:

相对于图例中的文本,符号的垂直调整

英文:

The easiest way is to put a blank line before the text. That way, the first line of text will be aligned with the box:

pie(table(iris$Species), col = palette.colors(3, "Pastel 1"), cex = 2)
legend('right',c('A','B',' \nlong\ntext'), fill = palette.colors(3, "Pastel 1"), 
       cex = 2, box.lty = 0, y.intersp = 0)

相对于图例中的文本,符号的垂直调整

答案2

得分: 0

iris <- iris
ggplot(iris, aes(x=Species, fill=Species)) +
geom_bar() +
scale_fill_manual(values=colors(3), labels=c('A', 'B', '长文本')) +
theme(legend.position="右侧", legend.justification=0.5)

相对于图例中的文本,符号的垂直调整

英文:
iris &lt;- iris
ggplot(iris, aes(x=Species, fill=Species)) + 
  geom_bar() + 
  scale_fill_manual(values=colors(3), labels=c(&#39;A&#39;, &#39;B&#39;, &#39;long\ntext&#39;)) +
  theme(legend.position=&quot;right&quot;, legend.justification=0.5)

相对于图例中的文本,符号的垂直调整

huangapple
  • 本文由 发表于 2023年2月6日 03:52:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定