在ggplot2中使用facet_wrap时,通过直线在两个轴上跟踪相同的值

问题描述 投票:1回答:1

我有这个数据框:

df <- structure(list(Real = c(5, 11, 11, 17, 6, 7, 11, 11, 10, 11, 
13, 11, 7, 8, 8, 9, 10, 7, 7, 6, 5, 13, 8, 10, 7, 11, 4, 6, 5, 
7, 9, 4, 8), Predicted = c(7.53104960942489, 8.48933218073805, 
6.53527751338467, 15.5681067019339, 6.16286447784273, 9.60252366514763, 
9.42571629457248, 10.2524454322055, 9.32847653546665, 9.77523370651072, 
10.3625498760624, 14.275897279353, 7.69457109139684, 7.26906167591947, 
8.07512249762137, 7.36859387534307, 10.357436237549, 7.87435765638371, 
8.74311433109198, 7.29886361235044, 7.2713976024488, 7.55669872113011, 
7.43176240945188, 13.9422570019077, 9.88336055293638, 9.63284506089816, 
5.5285713215912, 10.4119762640024, 4.76896313240313, 8.45958527511939, 
5.65034115485842, 4.12733609483675, 4.27125334429662), Tooth = c("Incisor", 
"Incisor", "Incisor", "Incisor", "Incisor", "Canine", "Canine", 
"Canine", "Canine", "Canine", "Canine", "Canine", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Premolar", "Premolar", "Premolar", "Premolar", "Premolar", 
"Premolar", "Molar", "Molar", "Molar", "Molar", "Molar", "Molar", 
"Molar")), .Names = c("Real", "Predicted", "Tooth"), row.names = c("167", 
"174", "176", "315", "321", "105", "148", "241", "272", "305", 
"314", "337", "115", "118", "131", "141", "144", "224", "227", 
"249", "253", "258", "275", "306", "323", "338", "228", "295", 
"299", "311", "312", "317", "326"), class = "data.frame")

并且我运行此ggplot:

int_breaks <- function(x, n = 5) {
  l <- pretty(x, n)
  l[abs(l %% 1) < .Machine$double.eps ^ 0.5] 
}

library(ggplot2)
ggplot(data = df, aes(x = Real, y = Predicted)) +
  geom_point(color = "red") +
  facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")), scales = "free") +
  scale_y_continuous(breaks = int_breaks) +
  scale_x_continuous(breaks = int_breaks)

我终于得到了这个情节:

enter image description here

我想在每个图中绘制一条通过X和Y轴相同值的线。也就是说,坐标(10,10)和(15,15)将定义线。

预期结果将是这样:

enter image description here

我尝试了geom_abline(),没有退出。

而且,我意识到使用coord_fixed()时出现错误。关于如何标准化可视化的任何想法?

r ggplot2 facet-wrap
1个回答
0
投票

geom_abline的这种方法对您不起作用吗?

library(ggplot2)
ggplot(data = df, aes(x = Real, y = Predicted)) +
  geom_point(color = "red") +
  geom_abline(slope = 1, intercept = 0) +
  facet_wrap(~factor(Tooth, levels = c("Incisor", "Canine", "Premolar", "Molar")), scales = "free") +
  scale_y_continuous(breaks = int_breaks) +
  scale_x_continuous(breaks = int_breaks)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.