如何使用ggplot来对齐我的两个标签? [英] How to use ggplot to align my two labels?

查看:172
本文介绍了如何使用ggplot来对齐我的两个标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个标签的ggplot图。我想将两个标签添加到我的情节,垂直对齐。我希望我的第二个标签可以绘制在第一个标签的下面,但它们不应该重叠。

因此,如果我想在第(20,90)处绘制第一个标签,那么应该在哪里第二个标签去?显然,它取决于数据集的y轴和第一个标签所需的高度。



问:坐标是硬编码的(参见我的脚本),不适用于新的数据集。我想提出一种方法,使两个标签总是垂直对齐,用于任何数据集和缩放。我的潜在解决方案是:


  1. 创建一个显示两行的标签,如 Line1\\\
    Line2
    使用布局算法来强制对齐,如 gridExtra 中的 tableGrob code> package。

我不知道如何使 1 起作用。在 2 中,我不确定如何使 tableGrob 可以使用单个标签。什么是最简单的解决方案?

  library(ggplot2)

data < - data.frame(x = c(1:100),y = x + rnorm(100))

p < - ggplot(data = data,aes_string(x ='data $ x',y ='data $ y ')+ geom_point()

str1< - italic(y)== \1.7 \+ \1 \* italic(x)* \ ,italic(r)^ 2=\\〜\\0.91 \\
str2 < - italic(y)== \9.7 \ + \1 \* italic(x)* \,\〜〜italic(r)^ 2〜\= \〜\9.91 \$ b $
以上< - 注释(text,
label = str1,
x = 20,
y = 90,
parse = TRUE)
以下< - 注释(text,
label = str2,
x = 20,
y = 83,
parse = TRUE)

p < - p +以上
p < - p +以下

print(p)


解决方案

您可以使用 at

'itopic(y)== \1.7 \+ \1 \* italic(x)* \,\〜〜italic(r)^ 2〜\= \〜\0.91 \,
italic(y)== \9.7 \+ \1 \* italic(x)* \,\ ~~'italic(r)^ 2〜\= \〜\9.91 \)'

p + annotate(text,
label = str3,
x = 20,
y = 90,
parse = TRUE)



您仍然需要输入单个标签的坐标。


I have a ggplot plot with two labels. I want to add both labels onto my plot, aligned vertically. I want my second label drawn below the first label, but they should not overlap.

So if I want to draw the first label at (20,90), where should the second label go? Obviously, it depends on the y-axis of the data set and the height required for the first label.

Q: The coordinates are hard-coded (see my script) and won't work for a new data set. I want to come up with a way such that the two labels are always aligned vertically for any data set and scaling. My potential solutions are:

  1. Create a single label showing two lines, something like Line1\nLine2
  2. Use a layout algorithm to force the alignment, like tableGrob from the gridExtra package.

I don't know how to make 1 works. In 2, I'm not sure how to make tableGrob works with individual labels. What's the easiest solution?

library(ggplot2)

data <- data.frame(x=c(1:100), y=x+rnorm(100))

p <- ggplot(data=data, aes_string(x='data$x', y='data$y')) + geom_point()

str1 <- "italic(y) == \"1.7\" + \"1\" * italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"0.91\""
str2 <- "italic(y) == \"9.7\" + \"1\" * italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"9.91\""

above <- annotate("text",
              label=str1,
              x=20,
              y=90,
              parse=TRUE)
below <- annotate("text",
              label=str2,
              x=20,
              y=83,
              parse=TRUE)

p <- p + above
p <- p + below

print(p)

解决方案

You could use atop to make single label out of the two labels, stacked one atop the other:

str3 = 'atop(italic(y) == \"1.7\" + \"1\" * italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"0.91\",
    italic(y) == \"9.7\" + \"1\" * italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"9.91\")'

p + annotate("text",
               label=str3,
               x=20,
               y=90,
               parse=TRUE)

You do still need to enter the coordinates for the single label.

这篇关于如何使用ggplot来对齐我的两个标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆