我试着用geom_text标记ggplot2中qq图上的点.aesthetic evaluation使用after_stat函数.计算出的统计数据标记为理论数据,如geom_qq computed variables的文档中所示,并应可用于geom_text?这是我第一次使用after_stat,所以不知道我做错了什么.

以下是我try 的一个例子:

library(ggplot2)
dat <- iris
dat$row <- 1:nrow(dat)
dat |> 
  ggplot(aes(sample = Petal.Length)) +
  geom_qq() +
  geom_qq_line() +
  geom_text(aes(label = row,
                x = after_stat(theoretical),
                y = after_stat(sample))) #Trying to label points on qq plot
#> Error in `geom_text()`:
#> ! Problem while mapping stat to aesthetics.
#> ℹ Error occurred in the 3rd layer.

也试过这种方式修改而不是geom_qqgeom_text

dat |> 
  ggplot(aes(sample = Petal.Length, label = row)) +
  geom_qq(geom = "text") +
  geom_qq_line()
#> Warning: The following aesthetics were dropped during statistical transformation: label.
#> ℹ This can happen when ggplot fails to infer the correct grouping structure in
#>   the data.
#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
#>   variable into a factor?
#> Warning: The following aesthetics were dropped during statistical transformation: label.
#> ℹ This can happen when ggplot fails to infer the correct grouping structure in
#>   the data.
#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
#>   variable into a factor?
#> Error in `geom_qq()`:
#> ! Problem while setting up geom.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_geom_1()`:
#> ! `geom_text()` requires the following missing aesthetics: label.

推荐答案

正如@Axeman已经提到的,您还必须在geom_text中使用stat="qq",才能基于值sampletheoretical向qqploy中添加标签.

然而,通常情况下,统计数据会转换原始数据集,并且一些美感会被完全丢弃,例如,stat="qq"只理解或支持xygroupsample,因此在统计转换之前所有其他美感都会被丢弃.因此,label=个AES被丢弃,这意味着在转换后的数据中不再有label列(如果您想要使用color个AES来例如按Species着色,也会发生同样的情况).因此,我们得到一个AN错误,因为geom_text需要label=个AES.

相反,当使用stat="qq"时,根据原始数据的row列标记点的一个选项是将row列按Petal.Length排序,即映射到sample上的变量,然后使用after_stat将重新排序的列映射到label个aes上,即使用after_stat(dat$row[order(dat$Petal.Length)]):

library(ggplot2)
dat <- iris
dat$row <- 1:nrow(dat)
dat |>
  ggplot(aes(sample = Petal.Length)) +
  geom_qq() +
  geom_qq_line() +
  geom_text(aes(
    label = after_stat(
      dat$row[order(dat$Petal.Length)]
    ),
    x = after_stat(theoretical),
    y = after_stat(sample + 1)
  ), stat = "qq", vjust = 0)

R相关问答推荐

列出用m n个值替换来绘制n个数字的所有方法(i.o.w.:R中大小为n的集合的所有划分为m个不同子集)

对lme 4对象运行summary()时出错(diag中的错误(from,names = RST):对象unpackedMatrix_diag_get找不到)

Highcharter多次钻取不起作用,使用不同方法

如何在R中对深度嵌套的tibbles中的非空连续行求和?

S用事件解决物质平衡问题

为什么当用osmdata映射R时会得到相邻状态?

通过在colname中查找其相应值来创建列

如何删除最后一个可操作对象

根据现有列的名称和字符串的存在进行变异以创建多个新列

计算数据帧中指定值之前的行数,仅基于每行之后的future 行,单位为r

在GG图中绘制射线的自动程序

R如何将列名转换为更好的年和月格式

在R中,如何将误差条放置在堆叠的每个条上?

通过初始的shiny 应用更新部署的shiny 应用的数据和参数,其中部署的应用程序显示为URL

按组和连续id计算日期差

有毒元素与表观遗传年龄的回归模型

将日期列从字符转换为日期得到的结果是NAS

合并多个数据帧,同时将它们的名称保留为列名?

Data.table条件合并

R:部分修改矩阵对角线的有效方法