我正在x从df在下面提供的工作示例中调用的数据帧调用的变量中构建分位数 - 分位数图.我想用name我的df数据集的变量标记点.
是否有可能在ggplot2中这样做而不诉诸痛苦的解决方案(手工编写理论分布然后根据经验分析绘制它)?
编辑:它发生了是的,感谢用户发布然后删除了他的答案.在下面的Arun回答之后看到评论.感谢Didzis的巧妙解决方案ggbuild.
# MWE
df <- structure(list(name = structure(c(1L, 2L, 3L, 4L, 5L, 7L, 9L,
10L, 6L, 12L, 13L, 14L, 15L, 16L, 17L, 19L, 18L, 20L, 21L, 22L,
8L, 23L, 11L, 24L), .Label = c("AUS", "AUT", "BEL", "CAN", "CYP",
"DEU", "DNK", "ESP", "FIN", "FRA", "GBR", "GRC", "IRL", "ITA",
"JPN", "MLT", "NLD", "NOR", "NZL", "PRT", "SVK", "SVN", "SWE",
"USA"), class = "factor"), x = c(-0.739390016757746, 0.358177826874146,
1.10474523846099, -0.250589535389937, -0.423112615445571, -0.862144579740376,
0.823039669834058, 0.079521521937704, 1.08173649722493, -2.03962942823921,
1.05571087029737, 0.187147291278723, -0.144770773941437, 0.957990771847331,
-0.0546549555439176, -2.70142550075757, -0.391588386498849, -0.23855544527369,
-0.242781575907386, -0.176765072121165, 0.105155860923456, 2.69031085872414,
-0.158320176671995, -0.564560815972446)), .Names = c("name",
"x"), row.names = c(NA, -24L), class = "data.frame")
library(ggplot2)
qplot(sample = x, data = df) + geom_abline(linetype = "dotted") + theme_bw()
# ... using names instead of points would allow to spot the outliers
Run Code Online (Sandbox Code Playgroud)
我正在努力改编这个要点,如果我对回归诊断有疑问,我会考虑向CrossValidated发送其他问题,这可能是CV用户感兴趣的.
您可以将原始QQ图保存为对象(使用的功能ggplot()而stat_qq()不是qplot())
g<-ggplot(df, aes(sample = x)) + stat_qq()
Run Code Online (Sandbox Code Playgroud)
然后使用功能,ggplot_build()您可以提取用于绘图的数据.它们存储在元素中data[[1]].将这些数据保存为新数据框.
df.new<-ggplot_build(g)$data[[1]]
head(df.new)
x y sample theoretical PANEL group
1 -2.0368341 -2.7014255 -2.7014255 -2.0368341 1 1
2 -1.5341205 -2.0396294 -2.0396294 -1.5341205 1 1
3 -1.2581616 -0.8621446 -0.8621446 -1.2581616 1 1
4 -1.0544725 -0.7393900 -0.7393900 -1.0544725 1 1
5 -0.8871466 -0.5645608 -0.5645608 -0.8871466 1 1
6 -0.7415940 -0.4231126 -0.4231126 -0.7415940 1 1
Run Code Online (Sandbox Code Playgroud)
现在,您可以添加观察数据框的名称.重要的是使用order()新数据框中的数据进行排序.
df.new$name<-df$name[order(df$x)]
Run Code Online (Sandbox Code Playgroud)
现在像往常一样绘制新数据框而不是geom_point()提供geom_text().
ggplot(df.new,aes(theoretical,sample,label=name))+geom_text()+
geom_abline(linetype = "dotted") + theme_bw()
Run Code Online (Sandbox Code Playgroud)

这些点太近了.我会做这样的事情:
df <- df[with(df, order(x)), ]
df$t <- quantile(rnorm(1000), seq(0, 100, length.out = nrow(df))/100)
p <- ggplot(data = df, aes(x=t, y=x)) + geom_point(aes(colour=df$name))
Run Code Online (Sandbox Code Playgroud)
这给出了:

如果你坚持在情节中使用标签,那么你可以尝试以下方法:
df <- df[with(df, order(x)), ]
df$t <- quantile(rnorm(1000), seq(0, 100, length.out = nrow(df))/100)
p <- ggplot(data = df, aes(x=t, y=x)) + geom_point(aes(colour=df$name))
p <- p + geom_text(aes(x=t-0.05, y=x-0.15, label=df$name, size=1, colour=df$name))
p
Run Code Online (Sandbox Code Playgroud)

你可以玩x和y坐标,如果你想你可以随时删除颜色美学.
| 归档时间: |
|
| 查看次数: |
4103 次 |
| 最近记录: |