将多个 `geom_text()` 分配给相应的 `facet_grid()`。

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

assign multiple geom_text() to corresponding facet_grid()

问题

我遵循了这个Stack Overflow [帖子][1],但没有解决我的问题。

假设我有一个非常大的数据框,一个样本可能如下所示:

    > df_test
        time                pT          model Obs Pred               auc            brier
    1  3-yrs pT3, pT4a or pT4b          18LNY 0.5  0.7  60.7 [46.9;74.4] 22.5 [17.8;27.2]
    2  3-yrs pT3, pT4a or pT4b          18LNY 0.3  0.2  60.7 [46.9;74.4] 22.5 [17.8;27.2]
    3  3-yrs        pT1 or pT2 LNY continuous 0.2  0.2  66.4 [55.4;77.4] 15.0 [11.0;19.0]
    4  5-yrs        pT1 or pT2 LNY continuous 0.6  0.5  73.2 [63.4;83.0] 17.3 [13.1;21.6]

我正在比较`df_test$model`中两个不同癌症阶段`df_test$pT`在`df_test$time`的三个不同时间点的预测。我使用`facet_grid(pT ~ time)`,输出了我期望的内容。

[![输入图片说明][2]][2]

现在,我想要添加一个`geom_text`,为每个`df$model`打印`df_test$auc`,即*每个*网格中的两个“auc读数”,一个用于`df_test$model == "18LNY"`,另一个用于`df_test$model == "LNY continuous"`。

如上面的样本所示,第1行和第2行打印相同的auc(因为它们属于相同模型的相同时间点)。因此,我已经将数据框减少到:

    label_test <- df_test %>% select(time, pT, model, auc) %>% distinct()

# 和
    > label_test
        time                pT          model               auc
    1  3-yrs pT3, pT4a or pT4b          18LNY  60.7 [46.9;74.4]
    2  3-yrs        pT1 or pT2 LNY continuous  66.4 [55.4;77.4]
    3  5-yrs        pT1 or pT2 LNY continuous  73.2 [63.4;83.0]
    4   1-yr pT3, pT4a or pT4b          18LNY  59.9 [44.6;75.1]
    5  5-yrs        pT1 or pT2          18LNY  73.5 [63.7;83.4]
    6  3-yrs        pT1 or pT2          18LNY  66.4 [55.3;77.5]
    7   1-yr        pT1 or pT2 LNY continuous 89.7 [77.7;100.0]
    8  3-yrs pT3, pT4a or pT4b LNY continuous  61.1 [47.3;74.9]
    9   1-yr pT3, pT4a or pT4b LNY continuous  59.7 [44.6;74.7]
    10 5-yrs pT3, pT4a or pT4b          18LNY  63.5 [48.7;78.4]
    11 5-yrs pT3, pT4a or pT4b LNY continuous  63.5 [48.6;78.4]

然后我尝试了以下内容:

    ggplot(df_test, aes(x = Pred, y = Obs, color = model)) + 
      facet_grid(time ~ pT) +
      geom_line() +
      geom_text(data = label_test,
                aes(x = 0, y = c(0.8, 0.9), label = auc), 
                size = 5, hjust = 0, show.legend = FALSE)

但出现了以下错误:

> `Error in `check_aesthetics()`: ! Aesthetics must be either length 1 or the same as the data (11): y`

如何解决?

数据

    df_test <- structure(list(time = c("3-yrs", "3-yrs", "3-yrs", "5-yrs", "1-yr", 
    "5-yrs", "1-yr", "3-yrs", "1-yr", "5-yrs", "1-yr", "3-yrs", "5-yrs", 
    "1-yr", "3-yrs", "5-yrs", "3-yrs", "3-yrs", "5-yrs", "5-yrs", 
    "3-yrs", "3-yrs", "3-yrs", "5-yrs", "3-yrs"), pT = c("pT3, pT4a or pT4b", 
    "pT3, pT4a or pT4b", "pT1 or pT2", "pT1 or pT2", "pT3, pT4a or pT4b", 
    "pT1 or pT2", "pT3, pT4a or pT4b", "pT1 or pT2", "pT1 or pT2", 
    "pT1 or pT2", "pT1 or pT2", "pT3, pT4a or pT4b", "pT1 or pT2", 
    "pT3, pT4a or pT4b", "pT3, pT4a or pT4b", "pT1 or pT2", "pT1 or pT2", 
    "pT1 or pT2", "pT3, pT4a or pT4b", "pT3, pT4a or pT4b", "pT3, pT4a or pT4b", 
    "pT3, pT4a or pT4b", "pT3, pT4a or pT4b", "p

<details>
<summary>英文:</summary>

I followed this SO [thread][1], which did not solve my problem.

Let&#39;s say I have a very large dataframe, of which a sample may look like:

    &gt; df_test
        time                pT          model Obs Pred               auc            brier
    1  3-yrs pT3, pT4a or pT4b          18LNY 0.5  0.7  60.7 [46.9;74.4] 22.5 [17.8;27.2]
    2  3-yrs pT3, pT4a or pT4b          18LNY 0.3  0.2  60.7 [46.9;74.4] 22.5 [17.8;27.2]
    3  3-yrs        pT1 or pT2 LNY continuous 0.2  0.2  66.4 [55.4;77.4] 15.0 [11.0;19.0]
    4  5-yrs        pT1 or pT2 LNY continuous 0.6  0.5  73.2 [63.4;83.0] 17.3 [13.1;21.6]

I am comparing prediction of two models in `df_test$model` in two different cancer stages `df_test$pT` at three different timepoints in `df_test$time`. I am using a `facet_grid(pT ~ time)`, which prints what I expect.

[![enter image description here][2]][2]


Now, I want to add a `geom_text` that print `df_test$auc` for each `df$model`, i.e. two &quot;auc readouts&quot; in *each* grid, one for `df_test$model == &quot;18LNY&quot;` and one for `df_test$model == &quot;LNY continuous&quot;`.  

As you can see in the sample above, line 1 and 2 prints the same auc (because it belongs to the same model at the same timepoint). So I have reduced the dataframe to:

    label_test &lt;- df_test %&gt;% select(time, pT, model, auc) %&gt;% distinct()
    
    # and
    &gt; label_test
        time                pT          model               auc
    1  3-yrs pT3, pT4a or pT4b          18LNY  60.7 [46.9;74.4]
    2  3-yrs        pT1 or pT2 LNY continuous  66.4 [55.4;77.4]
    3  5-yrs        pT1 or pT2 LNY continuous  73.2 [63.4;83.0]
    4   1-yr pT3, pT4a or pT4b          18LNY  59.9 [44.6;75.1]
    5  5-yrs        pT1 or pT2          18LNY  73.5 [63.7;83.4]
    6  3-yrs        pT1 or pT2          18LNY  66.4 [55.3;77.5]
    7   1-yr        pT1 or pT2 LNY continuous 89.7 [77.7;100.0]
    8  3-yrs pT3, pT4a or pT4b LNY continuous  61.1 [47.3;74.9]
    9   1-yr pT3, pT4a or pT4b LNY continuous  59.7 [44.6;74.7]
    10 5-yrs pT3, pT4a or pT4b          18LNY  63.5 [48.7;78.4]
    11 5-yrs pT3, pT4a or pT4b LNY continuous  63.5 [48.6;78.4]


I then tried 

    ggplot(df_test, aes(x = Pred, y = Obs, color = model)) + 
      facet_grid(time ~ pT) +
      geom_line() +
      geom_text(data = label_test,
                aes(x = 0, y = c(0.8, 0.9), label = auc), 
                size = 5, hjust = 0, show.legend = FALSE)

Which gives

&gt; Error in `check_aesthetics()`: ! Aesthetics must be either length 1 or
&gt; the same as the data (11): y

How to solve? 

Data

    df_test &lt;- structure(list(time = c(&quot;3-yrs&quot;, &quot;3-yrs&quot;, &quot;3-yrs&quot;, &quot;5-yrs&quot;, &quot;1-yr&quot;, 
    &quot;5-yrs&quot;, &quot;1-yr&quot;, &quot;3-yrs&quot;, &quot;1-yr&quot;, &quot;5-yrs&quot;, &quot;1-yr&quot;, &quot;3-yrs&quot;, &quot;5-yrs&quot;, 
    &quot;1-yr&quot;, &quot;3-yrs&quot;, &quot;5-yrs&quot;, &quot;3-yrs&quot;, &quot;3-yrs&quot;, &quot;5-yrs&quot;, &quot;5-yrs&quot;, 
    &quot;3-yrs&quot;, &quot;3-yrs&quot;, &quot;3-yrs&quot;, &quot;5-yrs&quot;, &quot;3-yrs&quot;), pT = c(&quot;pT3, pT4a or pT4b&quot;, 
    &quot;pT3, pT4a or pT4b&quot;, &quot;pT1 or pT2&quot;, &quot;pT1 or pT2&quot;, &quot;pT3, pT4a or pT4b&quot;, 
    &quot;pT1 or pT2&quot;, &quot;pT3, pT4a or pT4b&quot;, &quot;pT1 or pT2&quot;, &quot;pT1 or pT2&quot;, 
    &quot;pT1 or pT2&quot;, &quot;pT1 or pT2&quot;, &quot;pT3, pT4a or pT4b&quot;, &quot;pT1 or pT2&quot;, 
    &quot;pT3, pT4a or pT4b&quot;, &quot;pT3, pT4a or pT4b&quot;, &quot;pT1 or pT2&quot;, &quot;pT1 or pT2&quot;, 
    &quot;pT1 or pT2&quot;, &quot;pT3, pT4a or pT4b&quot;, &quot;pT3, pT4a or pT4b&quot;, &quot;pT3, pT4a or pT4b&quot;, 
    &quot;pT3, pT4a or pT4b&quot;, &quot;pT3, pT4a or pT4b&quot;, &quot;pT1 or pT2&quot;, &quot;pT1 or pT2&quot;
    ), model = c(&quot;18LNY&quot;, &quot;18LNY&quot;, &quot;LNY continuous&quot;, &quot;LNY continuous&quot;, 
    &quot;18LNY&quot;, &quot;18LNY&quot;, &quot;18LNY&quot;, &quot;18LNY&quot;, &quot;LNY continuous&quot;, &quot;18LNY&quot;, 
    &quot;LNY continuous&quot;, &quot;LNY continuous&quot;, &quot;LNY continuous&quot;, &quot;LNY continuous&quot;, 
    &quot;LNY continuous&quot;, &quot;LNY continuous&quot;, &quot;18LNY&quot;, &quot;LNY continuous&quot;, 
    &quot;18LNY&quot;, &quot;LNY continuous&quot;, &quot;18LNY&quot;, &quot;LNY continuous&quot;, &quot;LNY continuous&quot;, 
    &quot;LNY continuous&quot;, &quot;LNY continuous&quot;), Obs = c(0.5, 0.3, 0.2, 0.6, 
    0.2, 0.3, 0.3, 0.2, 0.4, 0.7, 0.4, 0.5, 0.5, 0.1, 0.5, 0.2, 0.1, 
    0.2, 0.6, 0.7, 0.5, 0.3, 0.3, 0.2, 0.6), Pred = c(0.7, 0.2, 0.2, 
    0.5, 0.2, 0.3, 0.3, 0.2, 0.2, 0.5, 0.5, 0.7, 0.4, 0.2, 0.5, 0.2, 
    0.1, 0.2, 0.6, 0.7, 0.6, 0.2, 0.3, 0, 0.8), auc = c(&quot;60.7 [46.9;74.4]&quot;, 
    &quot;60.7 [46.9;74.4]&quot;, &quot;66.4 [55.4;77.4]&quot;, &quot;73.2 [63.4;83.0]&quot;, &quot;59.9 [44.6;75.1]&quot;, 
    &quot;73.5 [63.7;83.4]&quot;, &quot;59.9 [44.6;75.1]&quot;, &quot;66.4 [55.3;77.5]&quot;, &quot;89.7 [77.7;100.0]&quot;, 
    &quot;73.5 [63.7;83.4]&quot;, &quot;89.7 [77.7;100.0]&quot;, &quot;61.1 [47.3;74.9]&quot;, 
    &quot;73.2 [63.4;83.0]&quot;, &quot;59.7 [44.6;74.7]&quot;, &quot;61.1 [47.3;74.9]&quot;, &quot;73.2 [63.4;83.0]&quot;, 
    &quot;66.4 [55.3;77.5]&quot;, &quot;66.4 [55.4;77.4]&quot;, &quot;63.5 [48.7;78.4]&quot;, &quot;63.5 [48.6;78.4]&quot;, 
    &quot;60.7 [46.9;74.4]&quot;, &quot;61.1 [47.3;74.9]&quot;, &quot;61.1 [47.3;74.9]&quot;, &quot;73.2 [63.4;83.0]&quot;, 
    &quot;66.4 [55.4;77.4]&quot;), brier = c(&quot;22.5 [17.8;27.2]&quot;, &quot;22.5 [17.8;27.2]&quot;, 
    &quot;15.0 [11.0;19.0]&quot;, &quot;17.3 [13.1;21.6]&quot;, &quot;13.2 [8.7;17.7]&quot;, &quot;17.2 [12.9;21.4]&quot;, 
    &quot;13.2 [8.7;17.7]&quot;, &quot;14.9 [10.9;18.9]&quot;, &quot;4.5 [2.1;6.8]&quot;, &quot;17.2 [12.9;21.4]&quot;, 
    &quot;4.5 [2.1;6.8]&quot;, &quot;22.4 [17.8;27.1]&quot;, &quot;17.3 [13.1;21.6]&quot;, &quot;13.3 [8.8;17.7]&quot;, 
    &quot;22.4 [17.8;27.1]&quot;, &quot;17.3 [13.1;21.6]&quot;, &quot;14.9 [10.9;18.9]&quot;, &quot;15.0 [11.0;19.0]&quot;, 
    &quot;24.0 [18.4;29.6]&quot;, &quot;23.9 [18.3;29.4]&quot;, &quot;22.5 [17.8;27.2]&quot;, &quot;22.4 [17.8;27.1]&quot;, 
    &quot;22.4 [17.8;27.1]&quot;, &quot;17.3 [13.1;21.6]&quot;, &quot;15.0 [11.0;19.0]&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
    -25L))


  [1]: https://stackoverflow.com/questions/15867263/ggplot2-geom-text-with-facet-grid
  [2]: https://i.stack.imgur.com/IkT5f.png

</details>


# 答案1
**得分**: 2

一种解决你的问题的选项是将标签的`y`位置添加到你的`labels_df`,然后将其映射到`y` aes。

```R
library(dplyr)
library(ggplot2)

label_test <- df_test %>%
  select(time, pT, model, auc) %>%
  distinct() %>%
  group_by(time, pT) %>%
  mutate(y = seq(.8, .9, length.out = n())) %>%
  ungroup()

ggplot(df_test, aes(x = Pred, y = Obs, color = model)) +
  facet_grid(time ~ pT) +
  geom_line() +
  geom_text(data = label_test,
            aes(x = 0, y = y, label = auc),
            size = 5, hjust = 0, show.legend = FALSE)

将多个 `geom_text()` 分配给相应的 `facet_grid()`。

英文:

One option to fix your issue would be to add the y position for the labels to your labels_df which could then be mapped on the y aes.

library(dplyr)
library(ggplot2)
label_test &lt;- df_test %&gt;% 
select(time, pT, model, auc) %&gt;% 
distinct() %&gt;% 
group_by(time, pT) %&gt;% 
mutate(y = seq(.8, .9, length.out = n())) %&gt;% 
ungroup()
ggplot(df_test, aes(x = Pred, y = Obs, color = model)) + 
facet_grid(time ~ pT) +
geom_line() +
geom_text(data = label_test,
aes(x = 0, y = y, label = auc), 
size = 5, hjust = 0, show.legend = FALSE)

将多个 `geom_text()` 分配给相应的 `facet_grid()`。

huangapple
  • 本文由 发表于 2023年2月8日 20:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385724.html
匿名

发表评论

匿名网友

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

确定