英文:
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's say I have a very large dataframe, of which a sample may look like:
> 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 "auc readouts" in *each* grid, one for `df_test$model == "18LNY"` and one for `df_test$model == "LNY continuous"`.
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 <- df_test %>% select(time, pT, model, auc) %>% distinct()
# and
> 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
> Error in `check_aesthetics()`: ! Aesthetics must be either length 1 or
> the same as the data (11): y
How to solve?
Data
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", "pT1 or pT2", "pT1 or pT2"
), model = c("18LNY", "18LNY", "LNY continuous", "LNY continuous",
"18LNY", "18LNY", "18LNY", "18LNY", "LNY continuous", "18LNY",
"LNY continuous", "LNY continuous", "LNY continuous", "LNY continuous",
"LNY continuous", "LNY continuous", "18LNY", "LNY continuous",
"18LNY", "LNY continuous", "18LNY", "LNY continuous", "LNY continuous",
"LNY continuous", "LNY continuous"), 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("60.7 [46.9;74.4]",
"60.7 [46.9;74.4]", "66.4 [55.4;77.4]", "73.2 [63.4;83.0]", "59.9 [44.6;75.1]",
"73.5 [63.7;83.4]", "59.9 [44.6;75.1]", "66.4 [55.3;77.5]", "89.7 [77.7;100.0]",
"73.5 [63.7;83.4]", "89.7 [77.7;100.0]", "61.1 [47.3;74.9]",
"73.2 [63.4;83.0]", "59.7 [44.6;74.7]", "61.1 [47.3;74.9]", "73.2 [63.4;83.0]",
"66.4 [55.3;77.5]", "66.4 [55.4;77.4]", "63.5 [48.7;78.4]", "63.5 [48.6;78.4]",
"60.7 [46.9;74.4]", "61.1 [47.3;74.9]", "61.1 [47.3;74.9]", "73.2 [63.4;83.0]",
"66.4 [55.4;77.4]"), brier = c("22.5 [17.8;27.2]", "22.5 [17.8;27.2]",
"15.0 [11.0;19.0]", "17.3 [13.1;21.6]", "13.2 [8.7;17.7]", "17.2 [12.9;21.4]",
"13.2 [8.7;17.7]", "14.9 [10.9;18.9]", "4.5 [2.1;6.8]", "17.2 [12.9;21.4]",
"4.5 [2.1;6.8]", "22.4 [17.8;27.1]", "17.3 [13.1;21.6]", "13.3 [8.8;17.7]",
"22.4 [17.8;27.1]", "17.3 [13.1;21.6]", "14.9 [10.9;18.9]", "15.0 [11.0;19.0]",
"24.0 [18.4;29.6]", "23.9 [18.3;29.4]", "22.5 [17.8;27.2]", "22.4 [17.8;27.1]",
"22.4 [17.8;27.1]", "17.3 [13.1;21.6]", "15.0 [11.0;19.0]")), class = "data.frame", 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)
英文:
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 <- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论